问题
How to display an error when the uploaded files are exceeding post_max_size
php?
print_r($_FILES);
I get an empty array when I have exceeded the post_max_size
array()
I got this from php.net but I don't understand it and don't know how to do it,
If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty. This can be tracked in various ways, e.g. by passing the $_GET variable to the script processing the data, i.e. , and then checking if $_GET['processed'] is set.
This is my form,
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" multiple="multiple" name="file[]" />
<input type="submit" name="upload" value="Submit"/>
</form>
回答1:
If $_SERVER['CONTENT_LENGTH']
is non zero and $_POST
and $_FILES
are empty, then the upload was too big.
回答2:
I have slightly modified toopay's code and I think you are looking for this. ini_get('post_max_size') helps you get the max size allowed.
$file_size = $_FILES['your-html-field-name']['size'];
$max_size = ini_get('post_max_size'); // Getting max size from php.ini
// Convert the file size to kilobytes
if($file_size > 0)
{
$file_size = round($file_size/1024, 2);
}
// Is the file size within the allowed maximum?
if ($file_size >= $max_size)
{
// set error flag or something...
// ...
return FALSE;
}
else
{
// continue process
// ...
return TRUE;
}
回答3:
You can write a simple size validation, to avoid that...
$max = count($_FILES['file']);
for ($i = 0; $i < $max; $i++)
{
$file_size = $_FILES['file']['size'][$i];
$max_size = 1000; // Define max size in Kb, for example 1Mb
// Convert the file size to kilobytes
if($file_size > 0)
{
$file_size = round($file_size/1024, 2);
}
// Is the file size within the allowed maximum?
if ($file_size > $max_size)
{
// set error flag or something...
// ...
return FALSE;
}
else
{
// continue process
// ...
return TRUE;
}
}
回答4:
Something like...
if ($_FILES["file"]["size"] < 2000000000)
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["ACfile"]["error"] . "<br />";
}
}
来源:https://stackoverflow.com/questions/7133067/how-to-display-an-error-when-the-uploaded-files-are-exceeding-post-max-size-php