Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecursiveDirectoryIterator() not showing empty directories

Tags:

php

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));
   }
 }
like image 425
Zachary K Avatar asked Aug 22 '11 14:08

Zachary K


1 Answers

Try:

$iterator =  new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator($root), RecursiveIteratorIterator::SELF_FIRST
);
like image 54
Yoshi Avatar answered Sep 20 '22 12:09

Yoshi