Question

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);
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top