【发布时间】:2014-04-15 17:20:57
【问题描述】:
我正在编写一个 Android 应用程序,我正在将文件上传到服务器,但我想找到一种方法来在服务器上创建一个新文件夹,而不是将文件上传到该文件夹。
我需要在应用程序中以编程方式创建文件夹,因为应用程序的每个用户都会在服务器上拥有自己的文件夹,该文件夹将在第一次上传文件时创建。
有人能给我一个建议吗?
到目前为止,这是我的 php 脚本:
<?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";
}
?>
这是我的应用程序中连接到它的代码:
//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);
【问题讨论】:
-
我会从这个想法开始:Android 应用程序会向某个安全的服务器 URL 发起一些请求,并且在该 URL 的服务器上运行的 PHP 将创建该文件夹。