sKunKbad Posted July 18, 2013 Share Posted July 18, 2013 I'm working on a file uploading script. It checks the file size, and won't let an upload complete if over a size specified in config. This all works, but if a file is uploaded that goes over PHP's limit, then I get the familiar warning: Warning: POST Content-Length of 14622352 bytes exceeds the limit of 8388608 bytes in Unknown on line 0 I am not looking to allow this file to go through. I don't want to increase memory or the max post size. That's not the issue. The upload script already has a way for the user to see if their file had problems uploading (showing errors), but how can I catch the error shown above? I know the error won't display if the error reporting and display of errors is turned off, but I don't want to leave the user in the dark. They should get some feedback. What can I do here? Link to comment https://forums.phpfreaks.com/topic/280285-how-do-i-catch-this-error/ Share on other sites More sharing options...
mac_gyver Posted July 18, 2013 Share Posted July 18, 2013 first check if a form has been submitted using $_SERVER['REQUEST_METHOD'] == "POST". then both the $_FILES and $_POST array will be empty for that condition or you can check $_SERVER['CONTENT_LENGTH'] to get the same information that is output in that warning message. Link to comment https://forums.phpfreaks.com/topic/280285-how-do-i-catch-this-error/#findComment-1441267 Share on other sites More sharing options...
sKunKbad Posted July 18, 2013 Author Share Posted July 18, 2013 first check if a form has been submitted using $_SERVER['REQUEST_METHOD'] == "POST". then both the $_FILES and $_POST array will be empty for that condition or you can check $_SERVER['CONTENT_LENGTH'] to get the same information that is output in that warning message. How about something like this: if( isset( $_SERVER["CONTENT_LENGTH"] ) && $_SERVER['REQUEST_METHOD'] == "POST" ) { if( $_SERVER["CONTENT_LENGTH"] > ( (int) get_cfg_var('post_max_size') * 1024 * 1024 ) ) { $this->error_stack[] = 'File size too large to upload.'; } } I'm using get_cfg_var because it works on all of the servers I tested, while ini_get didn't work on Litespeed. Link to comment https://forums.phpfreaks.com/topic/280285-how-do-i-catch-this-error/#findComment-1441269 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.