Jump to content

How do I catch this error?


sKunKbad
Go to solution Solved by sKunKbad,

Recommended Posts

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
Share on other sites

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.

Edited by mac_gyver
Link to comment
Share on other sites

  • Solution

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.