faqts : Computers : Programming : Languages : PHP : Function Libraries

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

19 of 22 people (86%) answered Yes
Recently 8 of 9 people (89%) answered Yes

Entry

How can I get all file names stored in a directory tree?
How can I recursively get the name and size of all files in a directory and it's subdirectories?

Aug 10th, 1999 11:00
Nathan Wallace, Robert Aden


I guess something like this would work. This functions returns the files 
and size of them in the form file1=size&file2=size2 so it needs some 
modification to just list them.

function sizeDirectory($directory) {
  $dirhandle = opendir($directory);
  while ( $entity = readdir($dirhandle)) {
    if ($entity[0] == '.') {
      // Do nutting unless dotfiles should count
    } elseif ( is_dir($directory . '/' . $entity)) {
      $ret .= sizeDirectory($directory . '/' . $entity);
    } else {
      $ret .= '&' . urlencode($entity) . '=' . (filesize($directory . 
'/' . $entity));
    }
  }
  return $ret;
}