How to gracefully handle files that exceed PHP `post_max_size`?

I am working on a PHP form that attaches a file to email and tries to gracefully handle cases where the downloaded file is too large.

I found out that in php.ini there are two parameters that affect the maximum file upload size: upload_max_filesize and post_max_size .

If the file size exceeds upload_max-filesize , PHP returns the file size as 0. That's fine; I can check it out.

But if it exceeds post_max_size , my script fails and returns to an empty form.

Is there any way to catch this error?

+82
php upload
Jan 25 '10 at 16:15
source share
5 answers

From the documentation :

If the mail data size is larger than post_max_size, $ _ POST and Superframes $ _FILES are empty . This can be tracked in various ways, for example. passing the variable $ _GET to the script data processing, that is, <form action = "edit.php? processed = 1"> and then checking if $ _GET ['processed'] set.

So, unfortunately, this is not like PHP is sending an error. And since it sends an empty $ _POST array, that’s why your script returns to an empty form - it doesn’t consider it to be POST. (Pretty bad IMHO design solution)

This commentator also has an interesting idea.

It seems like a more elegant way is to compare between post_max_size and $ _SERVER ['CONTENT_LENGTH']. please note that the latter includes not only the size of the uploaded file plus mail data but also multi-part sequences.

+52
Jan 25 '10 at 16:24
source share

there is a way to catch / process files that exceed the maximum message size, this is my preference, as it tells the end user what happened and who is to blame;)

 if (empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post') { //catch file overload error... $postMax = ini_get('post_max_size'); //grab the size limits... echo "<p style=\"color: #F00;\">\nPlease note files larger than {$postMax} will result in this error!<br>Please be advised this is not a limitation in the CMS, This is a limitation of the hosting server.<br>For various reasons they limit the max size of uploaded files, if you have access to the php ini file you can fix this by changing the post_max_size setting.<br> If you can't then please ask your host to increase the size limits, or use the FTP uploaded form</p>"; // echo out error and solutions... addForm(); //bounce back to the just filled out form. } else { // continue on with processing of the page... } 
+40
Jul 24 2018-11-11T00: 00Z
source share

We are having a problem with SOAP requests when checking for the void of $ _POST and $ _FILES does not work, because they are also empty in valid requests.

Therefore, we implemented verification by comparing CONTENT_LENGTH and post_max_size. The thrown exception is later converted to XML-SOAP-FAULT by our registered exception handler.

 private function checkPostSizeExceeded() { $maxPostSize = $this->iniGetBytes('post_max_size'); if ($_SERVER['CONTENT_LENGTH'] > $maxPostSize) { throw new Exception( sprintf('Max post size exceeded! Got %s bytes, but limit is %s bytes.', $_SERVER['CONTENT_LENGTH'], $maxPostSize ) ); } } private function iniGetBytes($val) { $val = trim(ini_get($val)); if ($val != '') { $last = strtolower( $val{strlen($val) - 1} ); } else { $last = ''; } switch ($last) { // The 'G' modifier is available since PHP 5.1.0 case 'g': $val *= 1024; // fall through case 'm': $val *= 1024; // fall through case 'k': $val *= 1024; // fall through } return $val; } 
+6
Jun 13 '14 at 9:44
source share

Based on the answers of @Matt McCormick and @AbdullahAJM, here is a PHP test case that checks the variables used in the test and then checks if the value of $ _SERVER ['CONTENT_LENGTH'] exceeds the value of the php_max_filesize parameter:

  if ( isset( $_SERVER['REQUEST_METHOD'] ) && ($_SERVER['REQUEST_METHOD'] === 'POST' ) && isset( $_SERVER['CONTENT_LENGTH'] ) && ( empty( $_POST ) ) ) { $max_post_size = ini_get('post_max_size'); $content_length = $_SERVER['CONTENT_LENGTH'] / 1024 / 1024; if ($content_length > $max_post_size ) { print "<div class='updated fade'>" . sprintf( __('It appears you tried to upload %d MiB of data but the PHP post_max_size is %d MiB.', 'csa-slplus'), $content_length, $max_post_size ) . '<br/>' . __( 'Try increasing the post_max_size setting in your php.ini file.' , 'csa-slplus' ) . '</div>'; } } 
+4
Mar 21 '15 at 1:58
source share

This is an easy way to solve this problem:

Just run "checkPostSizeExceeded" when you run your code

 function checkPostSizeExceeded() { if (isset($_SERVER['REQUEST_METHOD']) and $_SERVER['REQUEST_METHOD'] == 'POST' and isset($_SERVER['CONTENT_LENGTH']) and empty($_POST)//if is a post request and $_POST variable is empty(a symptom of "post max size error") ) { $max = get_ini_bytes('post_max_size');//get the limit of post size $send = $_SERVER['CONTENT_LENGTH'];//get the sent post size if($max < $_SERVER['CONTENT_LENGTH'])//compare throw new Exception( 'Max size exceeded! Were sent ' . number_format($send/(1024*1024), 2) . 'MB, but ' . number_format($max/(1024*1024), 2) . 'MB is the application limit.' ); } } 

Remember a copy of this helper function:

 function get_ini_bytes($attr){ $attr_value = trim(ini_get($attr)); if ($attr_value != '') { $type_byte = strtolower( $attr_value{strlen($attr_value) - 1} ); } else return $attr_value; switch ($type_byte) { case 'g': $attr_value *= 1024*1024*1024; break; case 'm': $attr_value *= 1024*1024; break; case 'k': $attr_value *= 1024; break; } return $attr_value; } 
+1
Nov 29 '16 at 18:28
source share



All Articles