How to display an error when the uploaded files ar

2019-08-01 08:17发布

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>

4条回答
唯我独甜
2楼-- · 2019-08-01 08:30

If $_SERVER['CONTENT_LENGTH'] is non zero and $_POST and $_FILES are empty, then the upload was too big.

查看更多
家丑人穷心不美
3楼-- · 2019-08-01 08:33

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;
    }
}
查看更多
Explosion°爆炸
4楼-- · 2019-08-01 08:34

Something like...

if ($_FILES["file"]["size"] < 2000000000)
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["ACfile"]["error"] . "<br />";
    }
}
查看更多
姐就是有狂的资本
5楼-- · 2019-08-01 08:45

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;
}
查看更多
登录 后发表回答