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

Hello,

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:

http://www.pasteit.in/117

I would appreciate any help.

Thank you.

Thanks.

$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] == ""){
        $img="img/dir.gif";
        }elseif($n[1]=="php"){
        $img="img/php.jpg";
        }elseif($n[1]=="zip"){
        $img="img/zip.gif";
        }elseif($n[1]=="gif"){
        $img="img/gif.gif";
        }elseif($n[1]=="html"){
        $img="img/html.gif";
        }elseif($n[1]=="ini"){
        $img="img/ini.gif";
        }elseif($n[1]=="jpg"){
        $img="img/jpg.gif";
        }elseif($n[1]=="txt"){
        $img="img/txt.gif";
        }elseif($n[1]=="exe"){
        $img="img/exe.gif";
        }else{
        $img="img/no.gif";
        }

I would condense this.


$types = array("php","zip","gif","html","ini","jpg","txt","exe");
$n= explode(".",$file);
if (count($n) == 1){
  $img="img/dir.gif";
} 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; })