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

573 Views Asked by At

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.

2

There are 2 best solutions below

6
SergeAx On BEST ANSWER

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.

0
Kurt Van den Branden On

It looks like either a timeout or a filesize issue. Default execution time of a php script is 30 seconds. Change it to i.e. 300 (5 minutes) in your php.ini file.

; Maximum execution time of each script, in seconds
max_execution_time=300

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize=32M

Also, enable error reporting by putting the following in the very beginning of your php script. It will give you a more detailed description of what is going wrong.

error_reporting(E_ALL);
ini_set('display_errors', 1);