The PHP manual has a section called Handling file uploads. That section has a subsection called Error Messages Explained. That subsection describes an error called "UPLOAD_ERR_INI_SIZE":
Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
However, in my experience, it's impossible to ever check for this particular error using UPLOAD_ERR_INI_SIZE because if a user ever does upload a file that exceeds the upload_max_filesize directive in php.ini, the $_FILES superglobal is empty. Want to test it for yourself? Save this as "upload_test.php" and then try to upload a file that's under the limit and then a file that's over the limit:
<?php
if (isset($_GET['submitted']) && $_GET['submitted'] === 'true')
{
echo 'Contents of $_POST:<hr><pre>';
print_r($_POST);
echo '</pre><hr>Contents of $_FILES:<hr><pre>';
print_r($_FILES);
echo '</pre><hr>';
exit;
}
$max_filesize_in_mib = min((int)(ini_get('upload_max_filesize')), (int)(ini_get('post_max_size')), (int)(ini_get('memory_limit')));
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHP Upload Test</title>
</head>
<body>
<h1>Upload a File (Maximum File Size: <?php echo $max_filesize_in_mib; ?> MiB)</h1>
<form action="upload_test.php?submitted=true" enctype="multipart/form-data" method="post">
<input type="file" name="upload_test">
<input type="hidden" name="random_field" value="You should see this field in the $_POST superglobal.">
<input type="submit" value="Upload">
</form>
</body>
</html>
So my question is this: what's the point of UPLOAD_ERR_INI_SIZE if you can never check for it?
This make sense when your
POST_MAX_SIZE
is bigger thanUPLOAD_MAX_FILESIZE
.I tried in an environment where
POST_MAX_SIZE
is128MB
, then i setUPLOAD_MAX_FILESIZE
to1MB
Here's what i got (as expected):
Although we don't get the size of the file, we do know that it's exceeding the
upload_max_filesize
.As with so many things, this constant can be traced back to a constant in the PHP source. In particular, line 66 in rfc1867.c.
It would seem that perhaps the person documenting the upload functionality was just copying and explaining each constant they saw registered in the source.
Is to track the error when it
The uploaded file exceeds the upload_max_filesize directive in php.ini.
You can track the error using
error_get_last
, or thetry .. catch