Page 1 of 1

Listing files and file sizes...

Posted: Fri Feb 02, 2007 3:31 pm
by jkashu
I'm altering a script I found that itterates through a directory and list the files and the file sizes. I changed the script to seach a seperate directory "data". The directory is stored in my currect directory.

The script works fine until I have it search the directory data, it returns the file names, but it doesnt return the filesize. Any hints?

Here is what I have already.

Code: Select all

theDirectory            = "data"; 
$listDirectories    = false; 

if(is_dir($theDirectory)) 
{ 
    echo "<table><tr><td>Name</td><td>Type</td><td>Size</td></tr>"; 
    $dir = opendir($theDirectory); 
    while(false !== ($file = readdir($dir))) 
    { 
        $type    = filetype($theDirectory ."/". $file); 
        if($listDirectories || $type != "dir") 
        { 
            echo "<tr><td>" . $file . "</td>"; 
            echo "<td>" . $type . "</td>"; 
            echo "<td>"; 
            if($type == "file") 
                echo filesize($file); 
            echo "</td></tr>"; 
        } 
    } 
    closedir($dir); 
    echo "</table>"; 
} 
else 
{ 
    echo $theDirectory . " is not a directory"; 
}
When I have the directory set to "." everything works fine, but it's only when I set it to data that I get the trouble.

Thanks!

Posted: Fri Feb 02, 2007 6:54 pm
by RobertGonzalez
Can you run the script and post the output you say you are getting?

Posted: Fri Feb 02, 2007 10:50 pm
by feyd
path given to filesize() wrong?

Posted: Sat Feb 03, 2007 1:11 pm
by jkashu
This is the output:


Name Type Size
2.txt file
Warning: filesize() [function.filesize]: stat failed for 2.txt in /text.php on line 18

1.txt file
Warning: filesize() [function.filesize]: stat failed for 1.txt in /text.php on line 18

3.txt file
Warning: filesize() [function.filesize]: stat failed for 3.txt in /text.php on line 18

Posted: Sat Feb 03, 2007 3:39 pm
by Ollie Saunders

Code: Select all

echo filesize($theDirectory ."/". $file);
...instead of...

Code: Select all

echo filesize($file);
?

Posted: Sat Feb 03, 2007 5:21 pm
by jkashu
That makes it work perfectly ole...
Thanks!