Page 1 of 1

reading folders and files

Posted: Fri Feb 07, 2003 3:24 am
by terji
Does anybody have a function/class that can read through a folder/file structure and "snif" every file it finds :?:

Posted: Fri Feb 07, 2003 3:29 am
by volka
you can do that with opendir/readdir and is_file/is_dir
http://www.php.net/manual/en/function.opendir.php

Posted: Fri Feb 07, 2003 3:42 am
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;