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);