View Single Post
Old 09-26-2009, 12:46 PM   #1 (permalink)
benton
The Contributor
 
Join Date: Apr 2008
Posts: 78
Thanks: 0
benton is on a distinguished road
Default Glob recursion not working right

I need to be able to read through all directories in a given path and list all of the files. I'm using the following code and it works for the top level but not sub-directories. For example, if there is this structure
Code:
dirA
 dirB
  fileB 
 fileA
If will enter into dirA and display fileA. It will also show that it is going into the sub-directory dirB but fileB is never listed. Would someone please point out what I'm missing or let me know if there is a better way to do this?

PHP Code:
function GetAllFiles(&$files$locn)
{
  
$dirs glob($locn '/*'GLOB_ONLYDIR);
   
  if(
is_array($dirs) && count($dirs) > 0)
  {
    foreach (
$dirs as $dir)
    {
      
$cwd getcwd();
      
chdir ($dir);      
      
      foreach (
glob("*") as $filename
      {
         if (
is_dir($filename))
         {
           echo 
'enter subdir '.$filename.'<br>';
           
GetAllFiles($files$dir '/'.$filename);
         }  
               
         echo 
'file '.$filename .'<br>';
         
$files[] = $filename;
      }
       
chdir ($cwd); 
    }
  }

benton is offline  
Reply With Quote