doudui2229 2014-04-15 17:20
浏览 25
已采纳

android:使用php在服务器上创建一个新文件夹[关闭]

I am writing an Android application where I am uploading files to a server, but I would like to find a way how to create a new folder on a server, to which the files would be uploaded instead.

I need the folder to be created programatically in the application, as every user of the app would have his own folder on the server, which would be created during the first upload of a file.

Could someone please give me an advice how to do that?

This is my php script so far:

<?php

    $file_path = "uploads/";

$new_dir_name = $_GET['myDirectory'];
$file_path .= $new_dir_name . '/';

if (!is_dir($file_path)) {
        mkdir($file_path);
    chmod($file_path, 0777);
}

    $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
        echo "success";
    } else{
        echo "fail";
    }
 ?>

And this is the code in my app to connect to it:

            //updated:

            String myDirectory = "nameofNewDir";
        upLoadServerUri = "myserver/upload.php"+"?myDirectory="+myDirectory;  

filePathOnServer = "http://users.ecs.soton.ac.uk/du1e11/uploads/"+myDirectory+"/";



        URL url = new URL(upLoadServerUri);

            // Open a HTTP  connection to  the URL
            conn = (HttpURLConnection) url.openConnection(); 
            conn.setDoInput(true); // Allow Inputs
            conn.setDoOutput(true); // Allow Outputs
            conn.setUseCaches(false); // Don't use a Cached Copy
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("ENCTYPE", "multipart/form-data");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            conn.setRequestProperty("uploaded_file", fileName); 

            dos = new DataOutputStream(conn.getOutputStream());

            dos.writeBytes("Content-Disposition: form-data; name=uploaded_file;filename= '"
                    + fileName + "'" + lineEnd);
  • 写回答

1条回答 默认 最新

  • dougang2749 2014-04-15 17:45
    关注

    At first you should have a writeable base directory where PHP would be able to create subfolders. Then in your upload server-side script use an mkdir() command and chmod() to create and set permissions to the directory accordingly, then move the uploaded file to that folder. When you upload the file via your android app, the folder will be automatically created and the file will be moved there.

    Here's the code for your particular case:

    <?php
    
        $file_path = "uploads/";
        $new_name = "new_folder"; // this is the new folder you'll create
        $file_path .= $new_name . '/';
        mkdir($file_path);
        chmod($file_path, 0777);
    
        $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
        if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
            echo "success";
        } else{
            echo "fail";
        }
     ?>
    

    In your question you say you don't know the name of the folder to create. Well, this should be done on PHP side. You should decide on the server what name of the folder to use. In your original code there is nothing that specifies the folder, so it's up to you to decide how you want to do it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题