phpandroidvideoandroid-async-httploopj

How to ensure file upload works with video using loopj AndroidAsyncHttp?


I am trying to upload a video file to my server from my android app using loopj AndroidAsyncHttp library. Uploading an image gives a "Success: 200" response whereas attempting to upload a video give either a 500 or 302 error response. This is my upload method:

    private void uploadVideoTwo(String filePath){

        AsyncHttpClient client = new AsyncHttpClient();

        File myFile = new File(filePath);
        RequestParams params = new RequestParams();
        try {
            params.put("file", myFile);
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Params Exception: " + e.getMessage());
        }

        client.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                Log.d(TAG, "Success: " + String.valueOf(statusCode));
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Log.d(TAG, "Failure: " + String.valueOf(statusCode));
            }
        });


    }

My PHP code looks like this:

    <?php 

$result = array("success" => $_FILES["file"]["name"]);

$name= $_FILES['file']['name'];

$tmp_name= $_FILES['file']['tmp_name'];

$path= "uploads/";

if (isset($name)) {

    if (empty($name))
    {
        $result = array("success" => "error uploading file");
    }
    else
    {
        if (move_uploaded_file($tmp_name, $path . $name)) {
            $result = array("success" => "File successfully uploaded");
        }
    }
}
echo json_encode($result);
?>

Android Studio logcat shows progress percentage during upload of video file but gives error response after it gets to 100%. I am using an intent to open the camera to capture a video with a limit of 30 seconds and a size limit of 5 mb. I have no idea why uploading an image works fine but not a video using the same script.


Solution

  • Check the value of post_max_size and upload_max_filesize in your php.ini file. Default value is only 2 megabytes, so you should change it to something more appropriate.

    To find out the location of php.ini on your own host just insert a call to phpinfo() inside any available script on your site and see the output in your browser.

    For popular cloud platforms:

    On Google Cloud Engine (standard or flexible environments) put php.ini file to your project's root (alongside app.yaml) with these lines inside:

    post_max_size = 128M upload_max_filesize = 128M

    On Heroku put .user.ini file with the same content as above in your app's root directory.