如何正确处理服务器错误? [重复]

This question already has an answer here:

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.

</div>

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.