1

Using PHP, I wish to list the files in a small folder, along with their filesizes (and optionally their modified dates)
The file names are displayed OK, but the file sizes appear to be null.
E.G.-

   $myDir = opendir("xxxxxxxx");    
   // get each entry    
   while($entryName = readdir($myDir)) {     
    if ($entryName != "." && $entryName != "..") {    
      echo $entryName . ' --- ' . filesize($entryName) . ' bytes<br/>';           
      $dirArray[] = $entryName;    
    }   
   }

This shows up as

file1 --- bytes
file2 --- bytes
etc....

Why isn't the filesize being picked up? (I'll deal with the filedate later) ?

0

1 Answer 1

2

You're looping through the files (and you get the names correctly) since you already got the files for that specific folder. But, when you try to get the filesize, you're not typing the complete path. Remember, you're inside a directory now.

So, supply the filesize() function the whole relative path to each file and it'll work fine:

$dir = "test";
$myDir = opendir($dir);    

while($entryName = readdir($myDir)) {     
    if ($entryName != "." && $entryName != "..") {
        echo $entryName . ' --- ' . filesize($test."/".$entryName) . ' bytes<br/>';
        $dirArray[] = $entryName;    
    } 
}

As a side note, the sizes appeared as "empty" because the function was actually erring out, but you probably have error reporting off, so nothing was showing.

It's really helpful to have it on (in development only, in production you should hide errors and log them to a file or the like), since you'll immediately know that somethings wrong, and most of the times the error messages pretty much spell out the solution to the problem for you.

For instance, the error you'd have gotten with error reporting on:

Warning: filesize(): stat failed for ... in ... on line xxx

To turn error reporting on for the duration of the script, add this line right after opening the PHP tag:

error_reporting(E_ALL);

And to turn it on more permanently in your php.ini, add, modify or uncomment (depending on your current settings) this line:

error_reporting = E_ALL

Read more about error reporting in the manual

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.