Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Warning: POST Content-Length of n bytes exceeds the limit of 3145728 bytes in Unknown on line 0

Tags:

php

I am quite surprise to find the above-mentioned error in my error log because I thought I have already done the necessary work to catch the error in my PHP script:

if ($_FILES['image']['error'] == 0)
{
 // go ahead to process the image file
}
else
{
 // determine the error
 switch($_FILES['image']['error'])
 {
  case "1":
  $msg = "Uploaded file exceeds the upload_max_filesize directive in php.ini.";
  break;
  ....
 }
}

In my PHP.ini script, the relevant settings are:

memory_limit = 128M
post_max_size = 3M
upload_max_filesize = 500K

I understand that the 3M is equivalent to 3145728 bytes and that this is what that is triggering the error. If the file size is above 500k but less than 3M, the PHP script would be able to run as per normal, issuing the error message in $msg as per case 1.

How do I catch this error instead of letting the script terminate abruptly with a PHP warning when post size exceeds post_max_size but still well within the memory limit? I have looked at similar questions here, here and here, but couldn't find an answer.

like image 975
Question Overflow Avatar asked Jul 31 '12 11:07

Question Overflow


2 Answers

Found an alternative solution that does not deal with the error directly. The following code is written by a software engineer Andrew Curioso in his blog:

if($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST) &&
     empty($_FILES) && $_SERVER['CONTENT_LENGTH'] > 0)
{
  $displayMaxSize = ini_get('post_max_size');

  switch(substr($displayMaxSize,-1))
  {
    case 'G':
      $displayMaxSize = $displayMaxSize * 1024;
    case 'M':
      $displayMaxSize = $displayMaxSize * 1024;
    case 'K':
       $displayMaxSize = $displayMaxSize * 1024;
  }

  $error = 'Posted data is too large. '.
           $_SERVER[CONTENT_LENGTH].
           ' bytes exceeds the maximum size of '.
           $displayMaxSize.' bytes.';
}

As explained in his article, when the post size exceeds post_max_size, the super global arrays of $_POST and $_FILES will become empty. So, by testing for these and by confirming that there is some content being sent using the POST method, it can be deduced that such an error has occurred.

There is actually a similar question here, which I didn't manage to find earlier.

like image 134
Question Overflow Avatar answered Nov 02 '22 11:11

Question Overflow


You could check it with javascript first before the upload even takes place?

// Assumed input for file in your HTML
<input type="file" id="myFile" />


//binds to onchange event of your input field
$('#myFile').bind('change', function() {
    alert(this.files[0].size);
});

You can also pop a try catch around it:

try 
{
    if (!move_uploaded_file( 'blah blah' )) 
    {
        throw new Exception('Too damn big.');
    }
    // Can do your other error checking here...
    echo "Upload Complete!";
} 
catch (Exception $e) 
{
    die ('File did not upload: ' . $e->getMessage());
}
like image 1
Fluffeh Avatar answered Nov 02 '22 13:11

Fluffeh