Error in PHP Code Trying to find out the filesize of a file


Can any one help me in fixing the code. I am not getting the file size of the required files using PHP. The script is supposed to scan all files in a folder and display next to each file its file size in kb. Here is the code:

I would appreciate any help.

Thank you.


$file_size_now = round(2,$filesize / 1024 * 100) / 100 . "Kb";

The first argument to round() is the number to round. You’ll always print 2 here.

if ($n[1] == “”){

This will generate an error. (Explode will return an array of size 1, so [1] does not exist, and will generate a Index Undefined error.)
Use count($n) == 1 instead.

        $n= explode(".",$file);
        if ($n[1] == ""){

I would condense this.

$types = array("php","zip","gif","html","ini","jpg","txt","exe");
$n= explode(".",$file);
if (count($n) == 1){
} elseif(in_array(strtolower($n[1]),$types)) {
  $img = "img/".strtolower($n[1]).".gif";
} else {
  $img = "img/no.gif";

(Makes it easier to add items to the list. Just use a .gif for each.)

Combine your identical elseifs in the second section (gif/jpg, zip/exe), and again, count instead of check for “”

Also might want to check for “.” and “…” in the first section of your while. (if($file == “.” || $file == “…”) { continue; })