How can I catch this error? (POST Content-Length ...)

When uploading an image, I get this error: (max 8mb image)

Warning: POST Content-Length of 14259306 bytes exceeds the limit of 8388608 bytes in Unknown on line 0 

How can I display this message on order? I want to say that I want to put this error in CSS style. thanks....

+7
source share
6 answers

In case of common errors, you need to install an error handler. See here for more details. BUT

If errors are executed before the script is executed (for example, when downloading files), a custom error handler cannot be called because it is not registered at that time.

+7
source

Maybe you want to try something like this.

 if (isset($_SERVER["CONTENT_LENGTH"])) { if ($_SERVER["CONTENT_LENGTH"] > ((int)ini_get('post_max_size') * 1024 * 1024)) { die('<script type="text/javascript">window.open("some page youre gonna handle the error","_self");</script>'); } } 
+7
source
 if ($_SERVER['CONTENT_LENGTH'] < 8380000) { ... your code } else { ... Your Error Message } 

You can also increase max size in php.ini

 post_max_size = 60M upload_max_filesize = 60M 
+5
source

Use the answer from @ Batu-Zet to check the code, and then make sure display_errors disabled in your php.ini :

 display_errors=Off 
0
source

You can limit the maximum size of uploaded files in two ways:

  1. Include <input type="hidden" name="MAX_FILE_SIZE" value="..."/> in the HTML form;
  2. Configure upload_max_filesize in php.ini.

If you try to upload a file, upload_max_filesize larger than upload_max_filesize PHP will display a warning before running the PHP script:

Warning: POST content length of 14259306 bytes exceeds the limit of 8388608 bytes in the "Unknown" field on line 0

$_FILES and $_POST['MAX_FILE_SIZE'] in this case will be empty. It seems that UPLOAD_ERR_INI_SIZE never works. Anyway, I never caught this in my code.

When the upload file is smaller than upload_max_filesize some elements of the $_FILES may contain UPLOAD_ERR_FORM_SIZE errors for files, UPLOAD_ERR_FORM_SIZE exceeds MAX_FILE_SIZE .

You can catch both situations with the following PHP script:

 <?php $warning = error_get_last(); if ($warning !== null && stripos($warning['message'], 'POST Content-Length of') !== false) { error_clear_last(); echo 'Upload is bigger than '.ini_get('upload_max_filesize'); } else if ($_FILES['input_name']['error'] == UPLOAD_ERR_FORM_SIZE) { echo 'Upload is bigger than '.$_POST['MAX_FILE_SIZE']; } 
0
source

Wrap the download in TRY / CATCH. Catch the error and process it in CATCH.

 try { :: file upload :: } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } 
-5
source

All Articles