2

I have a form that lets users upload files but I'm having problems detecting files that are too large.

I have the following set in php.ini:

upload_max_filesize = 10M
post_max_size = 20M

I understand that the standard way to detect if files are larger than the config file allows is to check $_FILES['file_name']['error']. However this only works for me if the actual file is greater than upload_max_filesize and smaller than post_max_size.

If the actual file size is greater than post_max_size then the $_FILES variable is removed and I get the log message:

PHP Warning: POST Content-Length of xxx bytes exceeds the limit of yyy ...

So the question: How do I detect if a user is uploading a file that is greater than post_max_size? I need to display a valid user message if this happens.

Thanks.

aksu
  • 5,221
  • 5
  • 24
  • 39
  • Maybe you could check the file size before starting to upload it. See http://www.dotnet-tricks.com/Tutorial/jquery/HHLN180712-Get-file-size-before-upload-using-jquery.html – hpelleti Feb 07 '14 at 20:27
  • Check out this [similar question](http://stackoverflow.com/questions/2133652/how-to-gracefully-handle-files-that-exceed-phps-post-max-size). – WindsofTime Feb 07 '14 at 20:28
  • Could you regard the code that you use in uploading? – SaidbakR Feb 07 '14 at 20:31

2 Answers2

0

I suggest you use set_error_handler() and check for E_USER_WARNING error + appropriate message, and then send it to a user, possibly via throwing an Exception, possibly some other way.

Eternal1
  • 5,447
  • 3
  • 30
  • 45
  • Also you can use @ to suppress error messages. See http://www.php.net/manual/en/language.operators.errorcontrol.php – Jhuliano Moreno Feb 07 '14 at 20:32
  • @JhulianoMoreno: But what would you want to put the `@` in front of …? In my understanding this warning is not raised by a specific line of code, but when the script is started … – CBroe Feb 07 '14 at 20:37
  • This error can't be suppressed, and initial question was that OP wants to actually present this error to a user, not hide it. – Eternal1 Feb 07 '14 at 20:42
0

Like this:

$filesize = $_FILES['file']['size'];
$limit = ini_get('post_max_size');
if($filesize > $limit) {
   trigger_error("POST Content-Length of $filesize bytes exceeds the limit of $limit bytes.", E_WARNING);
}
else {
  // upload file
}
aksu
  • 5,221
  • 5
  • 24
  • 39