开发者

How to display an error when the uploaded files are exceeding post_max_size php?

开发者 https://www.devze.com 2023-03-29 06:25 出处:网络
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

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开发者_C百科="Submit"/>
</form>


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


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;
}


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;
    }
}


Something like...

if ($_FILES["file"]["size"] < 2000000000)
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["ACfile"]["error"] . "<br />";
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号