0

When I try to upload a MP4 video with 16.9 MB size, using ajax async post to an PHP file the console triggers an error saying: POST http://website.com/proc_vids.php net::ERR_EMPTY_RESPONSE

I know for a fact that this problem is related with PHP memory_limit because when I set to 200 MB it's all fine but when I change it back to 100 MB this error happens.

I can't even get the POST to an PHP variable because as soon as the ajax post call is made it triggers the error without even doing anything on server side (PHP). Here is the ajax post code:

var proc = 1;
video = document.getElementById('preview_video').src;
    $.ajax({
        'async': true,
        'type': "POST",
        'global': false,
        'dataType': 'json',
        'url': "proc_vids.php",
        'data': {proc: proc, video: video}
    }).done(function () {
            //Do something
            });

PHP code:

$proc = $_POST['proc'];

if ($proc == 1){

//$video = $_POST['video'];
}

As you can see I commented the line where I pass the POST to a variable and still triggering the error.

What can I do to the video variable containing the base64 code to not expand consuming such high memory levels? Is there any alternatives without setting the memory_limit higher?

B1GB0Y
  • 43
  • 3
  • 12
  • please post your `proc_vids.php` code – cmorrissey Apr 28 '15 at 18:09
  • Updated with PHP code. – B1GB0Y Apr 28 '15 at 18:22
  • Have you tried increasing `post_max_size`? – Justinas Apr 28 '15 at 18:25
  • How can it be a post_max_size issue? If I set the memory_limit to 200 MB this problem doesn't happen but I can't use more than 100 MB on my shared hosting provider. – B1GB0Y Apr 28 '15 at 18:30
  • 16.9MB < 100MB, so i don't get how the setting change helps... – dandavis Apr 28 '15 at 18:40
  • I think the problem is that you are saving the data in the `$_POST` variable when for large files it is best to use `$_FILES` as it doesn't save them in memory but to a temp file. There is an answer here that saves an image which you should be able to modify for video http://stackoverflow.com/questions/19032406/convert-html5-canvas-into-file-to-be-uploaded – cmorrissey Apr 28 '15 at 18:42
  • It's probably something to do when passing the base64 code of the video and performing POST, probably expands too much consuming more than 100 MB memory. If you try to encode any video file with more than 16 MB you will see milions of charaters. – B1GB0Y Apr 28 '15 at 18:45
  • As you can see on my PHP code I don't do anything to the POST, it is commented! The problem is on passing the AJAX POST to the PHP file. – B1GB0Y Apr 28 '15 at 18:50
  • It doesn't matter if you don't do anything with it ... its still in the `$_POST` variable which uses memory. – cmorrissey Apr 28 '15 at 19:20
  • Maube you should check your IIS/apache your error logs? – Ruben Apr 28 '15 at 19:48
  • You are right cmorrissey, sending as FormData quite solved the problem! Thank you :) – B1GB0Y Apr 29 '15 at 10:09

1 Answers1

0

Problem solved thanks to cmorrissey!

I used the same method as described in this thread: Convert HTML5 Canvas into File to be uploaded?

Sending AJAX POST as a FormData and converting the base64 data to Uint8Array into a blob is the key to not allocate PHP memory when the POST is made. But be careful tho because older browsers may not support blob.

Thank you guys ;)

Community
  • 1
  • 1
B1GB0Y
  • 43
  • 3
  • 12