6

I am trying to use PHP's RecursiveDirectoryIterator to list a set of directories. The issue I am having is that it seems to be skipping any empty directory it hits. Any ideas how to make it list empty directories?

Here is a clip of the code I am using:

 $iterator =  new RecursiveIteratorIterator(
                        new RecursiveDirectoryIterator($root));
 $result = array();

 $prefix_length = strlen($root) + 1;
 foreach($iterator as $file)
 {      
   if($file->isDir())
   {

      $result[] = dirname(substr($file->__toString(), $prefix_length));
   }
 }
Zachary K
  • 3,205
  • 1
  • 29
  • 36

1 Answers1

10

Try:

$iterator =  new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator($root), RecursiveIteratorIterator::SELF_FIRST
);
Yoshi
  • 54,081
  • 14
  • 89
  • 103
  • Thanks for this! :) Can you explain why `~RecursiveIteratorIterator::LEAVES_ONLY` will not work? Not work means it will only list folders in the first level. – hek2mgl Apr 11 '13 at 03:02