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.

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

报告相同问题?

悬赏问题

  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元