简体   繁体   中英

If-Statement Help in PHP

So for some reason this just doesn't make sense to me.

What Im trying to do is display 1 of 2 things:

  1. If the filesize of just 1 image in the folder is too big, display the error message I have above.
  2. If all of the filesizes are ok, display a bit of HTML code

Also, is my threshold correct if I want the limit to be 5MB?

<?php
$threshold = 5368709120;
$path = 'dir/'.$username;
foreach (glob($path."/{*.gif,*.jpg,*.jpeg,*.png}",GLOB_BRACE|GLOB_NOSORT) as $filename)  
{
    $size = filesize($filename);
    if ($size > $threshold) {
        exit('One or more of your photos are larger than 5MB. Resize your photos and try again.');
    }
}
?>

No, your file limit is actually 5 gigabytes:

5 -> bytes = 5
5 * 1024 -> kilobytes = 5,120
5 * 1024 * 1024 -> megabytes = 5,242,880
5 * 1024 * 1024 * 1024 -> gigabytes => 5,368,709,120

For user friendliness, you should tell the user WHICH file is too large, as well as check ALL the files before exiting. Let's say the user didn't know there was a 5 meg limit, and uploaded 50 files. 49 are too large. You're just telling the user there's a problem, not what caused the problem. Now they have to re-upload a file, then do it again. now there's 48 files too big, and around they go.

Something like this would be more appropriate

$limit = 5 * 1024 * 1024; // 5 meg
$errors = array();

foreach (glob($path."/{*.gif,*.jpg,*.jpeg,*.png}",GLOB_BRACE|GLOB_NOSORT) as $filename)  
   if (filesize($filename) > $limit) {
      $errors[] = $filename
   }
}

if (count($errors) > 0) {
   echo "The following files are too large: <ul>";
   echo implode("</li><li>", $errors);
   echo "</ul>";
} else {
   echo "Everything A-OK!";
}

我将使用以下内容,以便始终清楚代码的意图:

$threshold = 5 * 1024 * 1024; // 5MB

Your problem is that you are not calling filesize() on the full path of the file, just on the file name. This means that if the files resides outside the current working directory - as it looks like it does - it won't work. Apparently this is untrue with glob() .

Regarding is my threshold correct if I want the limit to be 5MB , the simple way to make sure it is correct is the calculate it instead of hard-coding it:

$threshold = 1024 * 1024 * 5;

As it is, you were looking for files over 5 GB.

<?php
$threshold = 5 * 1024 * 1024; // 5MB
$path = 'dir/'.$username;
foreach (glob($path."/{*.gif,*.jpg,*.jpeg,*.png}",GLOB_BRACE|GLOB_NOSORT) as $filename)  
{
    $size = filesize($filename);
    if ($size > $threshold) {
        exit('One or more of your photos are larger than 5MB. Resize your photos and try      
again.');
    }
}
?>
//display html code here

Just add the html code anywhere after the foreach loop, since it has already passed the if //$size>$threshold check(and has gone through all the images in the for loop

Your code is correct, although your threshold is not. 5368709120 is 5 GiB , you want 5000000 .

mega is just another word for million.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM