Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$_FILES ... ['error'] == UPLOAD_ERR_INI_SIZE doesn't work?

Status
Not open for further replies.

Ghodmode

Programmer
Feb 17, 2004
177
0
0
NZ
I want to handle errors in a file upload form. The problem I'm encountering occurs when I try to upload a file that is larger than the post_max_size php.ini directive.

I want to use [tt]$_FILES['fileinputname']['error'] == UPLOAD_ERR_INI_SIZE[/tt] to determine whether or not I should show an error message. When I upload a file that's too large, both [tt]$_POST[/tt] and [tt]$_FILES[/tt] are empty.

I get the following error in my server's error log:
Code:
PHP Warning:  POST Content-Length of 10134524 bytes exceeds the limit of 8388608 bytes in Unknown on line 0, referer: [URL unfurl="true"]http://home/script.php[/URL]

How can I properly detect and report this error?

Thank you.


--
-- Ghodmode

Give a man a fish and he'll come back to buy more... Teach a man to fish and you're out of business.
 
that's because the error message relates to the max POST size rather than the maximum upload file size etc.

you need to change your POST_MAX_SIZE directive in php.ini (or httpd.conf or .htaccess).

you could test for the error if you wanted. but it's very ugly and definitely not recommended. something like this

Code:
ini_set('display_errors', 'off'); //you probably need to set this by hand in php.ini
$lE = error_get_last();
if (  !empty($lE) &&  strpos($lE['message'] , 'POST Content-Length' ) !== false){
 die ('Naughty naughty.  you can only upload xxxxx bytes');
}
 
Thanks jpadie. Unfortunately, that didn't work for me.

Errors were already not showing in the page. Although the documentation says that the default value for the php.ini directive "display_errors" is "1", when I use ini_get('display_errors'), it returns null. I was reading the errors in the web server's error log.

error_get_last() returned another, unrelated, error (which I should probably fix anyway).

The only thing that worked for me was to set an arbitrarily large post_max_size in my .htaccess file for the directory containing the script.

I don't like this solution because it has the potential to fail if someone attempts to upload an even larger file. Just in case, I added a GET variable to the form's action to indicate it had been submitted. This was recommended in the documentation:
Of course, this solution also presents its own set of potential problems... What would I do for a web server other than Apache? ... what would I do on a server where the AllowOverrides httpd.conf directive is set to None?

Thank you.

--
-- Ghodmode

Give a man a fish and he'll come back to buy more... Teach a man to fish and you're out of business.
 
Try this and see if it helps:
Code:
foreach($_FILES['filename']['error'] as $key=>$error)
  if ($error == UPLOAD_ERR_INI_SIZE) {
    print "Too big!" ;
  } else {
    // process file
  }
}
Alternatively, try doing a print_r or a var_dump on the $_FILES array and see what you get.



Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top