Advertencia de PHP: la longitud del contenido POST de n bytes excede el límite de 3145728 bytes en Desconocido en la línea 0

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 según 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 aquí, aquí y aquí, pero no pudo encontrar una respuesta.

preguntado el 31 de julio de 12 a las 12:07

Why don't you wrap your function in a try..catch ¿cuadra? -

error_reporting(E_ALL & ~E_WARNING); to switch off PHP warnings. -

@fdomig That should be the absolute last resort. -

@fdomig on a production system yes. But right now the OP is developing, which is when one should strive to avoid workarounds. -

@MichaelRobinson a solution is to provide your own error handler with something like this: stackoverflow.com/a/1241751/246051 -

2 Respuestas

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 y $_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 aquí, which I didn't manage to find earlier.

contestado el 23 de mayo de 17 a las 13:05

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());
}

Respondido 31 Jul 12, 12:07

The user may not have javascript enabled. - Desbordamiento de preguntas

+1 this might not fully solve the problem, but a good suggestion. - Raab

@QuestionOverflow Although true, but chances of that happening is 1 in a million as everything today needs JS to be enabled. Would be difficult to find someone with their JS disabled - aspirina

@aspirin, I understand. But I am looking for a more reliable server side solution. You wouldn't want to do form validation client side only, would you? - Desbordamiento de preguntas

Yes, I can adopt try and catch block, but is this the way to go? Because I thought exceptions are triggered due to bad coding and I want to avoid bad coding. - Desbordamiento de preguntas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.