Listing files and file sizes...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jkashu
Forum Commoner
Posts: 45
Joined: Tue Jan 30, 2007 12:00 pm

Listing files and file sizes...

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you run the script and post the output you say you are getting?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

path given to filesize() wrong?
jkashu
Forum Commoner
Posts: 45
Joined: Tue Jan 30, 2007 12:00 pm

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

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

Code: Select all

echo filesize($file);
?
jkashu
Forum Commoner
Posts: 45
Joined: Tue Jan 30, 2007 12:00 pm

Post by jkashu »

That makes it work perfectly ole...
Thanks!
Post Reply