reading folders and files

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
terji
Forum Commoner
Posts: 37
Joined: Tue May 14, 2002 5:27 pm
Location: Denmark

reading folders and files

Post by terji »

Does anybody have a function/class that can read through a folder/file structure and "snif" every file it finds :?:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you can do that with opendir/readdir and is_file/is_dir
http://www.php.net/manual/en/function.opendir.php
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

Code: Select all

$files = getAllFiles(".");    // dir goes here
while (list ($key, $val) = each ($files)) 
    echo "$val<BR>\n";


function getAllFiles ($dir) &#123;
      $files = array();
      $single_files = array();

      $d = opendir ("$dir");

      //list the files in the dir
      while ($file = readdir ($d)) &#123;

            if ($file == ".." || $file == ".") continue;
            if (is_dir ($dir."/".$file)) &#123;
                  $dirFiles = getAllFiles($dir."/".$file);
                  foreach ($dirFiles as $f)
                        $files&#1111;] = $file."/".$f;
            &#125; else &#123;
                  $single_files&#1111;] = $file;
           &#125;

      &#125;

      closedir ($d);
      sort($files);
      sort($single_files);
      return array_merge($single_files,$files);
&#125;
Post Reply