-5

I face a big problem and would like you to help me solve one.

While uploading files on server I receive the "POST Content-Length of 323344106 bytes exceeds the limit of 5242880 bytes in Unknown on line 0" error. I know a meaning of the error, but I want to catch the error and show it to users; namely, I wanna show the error, for example, below the HTML form in order that users can understand why to receive the error.

Hit-or-miss
  • 508
  • 1
  • 6
  • 16

1 Answers1

1

This error cannot be caught as it happens before script execution. An exception is not raised, so you cannot use a try catch block.

See: http://us2.php.net/set-error-handler

If errors occur before the script is executed (e.g. on file uploads) the custom error handler cannot be called since it is not registered at that time.

My recommendations are the following:

  • Set upload_max_filesize and post_max_size to be significantly greater than the maximum allowable upload size you are targeting. Any amount less than upload_max_filesize and post_max_size but greater than your allowable size can be caught and an error returned.

  • Make it clear to the user what your allowable upload size is.

  • Files greater than upload_max_filesize and post_max_size will produce that error, so set display_errors = off in production. In this cases, a blank page will be shown. Show the user what the maximum upload size is with the upload form, so that the chance of this happening is reduced.

F21
  • 32,163
  • 26
  • 99
  • 170
  • Ah! Great answer :) Thanks for explaining why try catch cant be used. – Shivam Verma Jul 12 '14 at 09:06
  • I understand) If I add an input buffering I'll be able to catch a text of an error by means of the "ob_get_contents()" function after I can clear up the buffer and I'll be able to show the error on any place of webpage but I'm not sure that the solution is right. – Hit-or-miss Jul 12 '14 at 09:25
  • @user3024502 The problem is that once the error is encountered (it happens before processing of your script starts), it is impossible to handle it as your script will not be executed. – F21 Jul 12 '14 at 13:39