Can you use a catch statement for warnings: PHP

Viewed 40

I've recently been looking at an issue with a PHP file upload script when the file size is greater than 32mb since my webserver doesn't allow files larger than that to be uploaded. It immediately kicks back a warning on line 0 with the following text "PHP Warning: POST Content-Length of 52429281 bytes exceeds the limit of 33554432 bytes in Unknown on line 0".

I was wondering if it is possible to use a try-catch statement or something similar to alleviate this issue and block the main portion of my program from running. It is just a warning which is why I haven't done it yet. Any help is appreciated!

1 Answers

It's not possible to catch this warning because it's triggered before your code begins executing. You can see that the error line is even shown as 0. When the POST request goes over the maximum limit, the request is broken and the post data becomes empty.

In a more advanced case, you may try to read the raw post data directly from the php://input stream like this example and have full control, but that will require some changes in your server's default configuration. Note that this method will probably not work with files uploaded using multipart/form-data by normal html forms, you'll need to use javascript.

You can also suppress the warning message using display_startup_errors Off in the php.ini or even .htaccess if you are using apache, although I wouldn't recommend it.

Related