Fast Android Networking - Upload file and receive it using php code

802 Views Asked by At

I am trying to upload a file to my server using Fast Android Networking library. This is my android code which seams to work well.

private static void uploadZipFile(File zipFile,String TAG,String public_key)
    {
        AndroidNetworking.upload(CloudData.UPLOAD_FILE_TO_SERVER_URL)
                .addMultipartFile("file",zipFile)
                .addMultipartParameter("public_key",public_key)
                .setTag(TAG)
                .setPriority(Priority.HIGH)
                .build()
                .setUploadProgressListener((bytesUploaded, totalBytes) ->
                {
                    Log.e("Progress",(bytesUploaded / totalBytes)*100 + " % total size: " + totalBytes/1024 + " kb");
                })
                .getAsJSONObject(new JSONObjectRequestListener() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.e("onResponse",response);

                    }
                    @Override
                    public void onError(ANError error) {
                        Log.e("Bazinga","Upload ERROR");
                        error.printStackTrace();
                    }
                });
    }

Here is the .php code I use which I got it from here.

<?php

$target_dir = "uploads/";
$response = array(); 
$target_file_name = $target_dir.basename($_FILES["file"]["name"]);

$public_key = $_POST["public_key"]; 

if(isset($_FILES["file"]))
{
if(move_uploaded_file($_FILES["file"]["temp_name"],$target_file_name))
{
$success = true;
$message = "Uploaded!!!";
}
else
{
$success = false;
$message = "NOT Uploaded!!! _ Error While Uploading";
}
}
else{
$success = false;
$message = "missing field";
}
$response["success"] = $success;
$response["message"] = $message; 
$response["target_file_name"] = $target_file_name;

echo json_encode($response); 
?>

The result when I am running this it's:

{"success":false,"message":"NOT Uploaded!!! _ Error While Uploading","target_file_name":"uploads/data.zip"}

What am I doing wrong?

1

There are 1 best solutions below

0
On

The mistake was in this:

"temp_name"

it should be:

 "tmp_name"