roarster
roarster

Reputation: 4086

Android Multipart Image Upload Issues

I've been trying to upload an image file from android to my php server, but the $_FILES array in php is always empty. To test if it was a server or client issue I made a quick web-form that submitted the same data as the android app, which worked, making me think that there's a problem with the android code.

My php code is:

//check for uploaded files
var_dump($_POST);
var_dump($_FILES);

$target_path  = "./images/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
 echo "The file ".  basename( $_FILES['uploadedfile']['name']).
 " has been uploaded";
} else{
 echo "There was an error uploading the file, please try again!";
}

and the android part:

public void uploadFile(){

        File file = new File("/storage/sdcard0/Android/data/xxx/files/Pictures/JPEG20131005.jpg");


        RequestParams params = new RequestParams();
        try {
            params.put("uploadedfile", file);
        } catch(FileNotFoundException e) {
             Log.e("testcase", "file not found");
        }


        AsyncHttpClient client = new AsyncHttpClient();
        client.post("http://www.example.com/fileUploader.php", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                System.out.println(response);
            }
        });

}

I just get back

array(0){
}

for both var_dump($_POST); and var_dump($_FILES);

This is my form code which works:

<form action="fileUploader.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="uploadedfile" id="uploadedfile"><br>
<input type="submit" name="submit" value="Submit">
</form>

I've also tried this example and a few others which all seem to share similar code but I got the same result so I've settled with using loopJ, purely because it's a tried and tested library.

Upvotes: 1

Views: 1351

Answers (1)

roarster
roarster

Reputation: 4086

Ok, so it turns out the url i was sending the data to started with a www. and the server was giving a 302 redirect to the same domain, without the www. This was in turn removing all the data for some reason. So after a few days of hair pulling, it was just 4 simple backspaces....

Upvotes: 1

Related Questions