1

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>
hakre
  • 193,403
  • 52
  • 435
  • 836
Run
  • 54,938
  • 169
  • 450
  • 748

4 Answers4

3

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;
}
Muhammad Usman
  • 12,439
  • 6
  • 36
  • 59
  • Thanks but I have this error `Fatal error: Unsupported operand types in C:\wamp\www\test\2011\javascript\jquery.multiple.upload\3\upload-test.php on line 7` which refers to `$file_size = ($file_size > 0) ? round($file_size/1024, 2) : 0;. Note that I am uploading multiple files. And this is my file input field is like - `` thanks. – Run Aug 20 '11 at 17:31
  • The fatal error is may be for any version problem I have edited the answer – Muhammad Usman Aug 20 '11 at 17:51
3

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

Matthew
  • 47,584
  • 11
  • 86
  • 98
1

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;
    }
}
toopay
  • 1,635
  • 11
  • 18
  • Thanks but I have this error `Fatal error: Unsupported operand types in C:\wamp\www\test\2011\javascript\jquery.multiple.upload\3\upload-test.php on line 7` which refers to `$file_size = ($file_size > 0) ? round($file_size/1024, 2) : 0;. Note that I am uploading multiple files. And this is my file input field is like - `` Thanks. – Run Aug 20 '11 at 17:32
0

Something like...

if ($_FILES["file"]["size"] < 2000000000)
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["ACfile"]["error"] . "<br />";
    }
}
Lee Armstrong
  • 11,420
  • 15
  • 74
  • 122