6

I am working with PHP's SPL Recursive Iterators, they are rather confusing to me though but I am learning.

I am using them in a project where I need to recursively grab all files and exclude folders from my result. I was initially using this method...

$directory = new RecursiveDirectoryIterator($path);
$iterator = new RecursiveIteratorIterator($directory,
                RecursiveIteratorIterator::CHILD_FIRST);

foreach ($iterator as $fileinfo) {    
    if ($fileinfo->isDir()) {
        //skip directories
        //continue;
    }else{
        // process files
    }
}

But then a SO user suggested that I use this method instead so that I would not need to use the isDir() method in my loop...

$directory = new RecursiveDirectoryIterator($path,
                        RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator($directory,
                        RecursiveIteratorIterator::LEAVES_ONLY);

Notice that I used RecursiveDirectoryIterator::SKIP_DOTS in the RecursiveDirectoryIterator constructor which is supposed to skip folders or . and ..

Now I am confused because after some test, even without using the RecursiveDirectoryIterator::SKIP_DOTS it seems to not show them, I am using Windows could that be the reason, do the dots only show up on a Unix type system? Or am I confused to the point that I am missing something?

Also by using RecursiveIteratorIterator::LEAVES_ONLY instead of RecursiveIteratorIterator::CHILD_FIRST it will stop folders from showing up in my result which is what I want but I don't understand why? The documentation has no information on this

hakre
  • 193,403
  • 52
  • 435
  • 836
CodeDevelopr
  • 1,267
  • 3
  • 17
  • 30
  • 2
    *(sidenote)* the default iteration mode is `LEAVES_ONLY`, so all you need to do is `new RecursiveIteratorIterator($directory);` - i will expand the docs for this – Gordon Jan 11 '12 at 12:47

1 Answers1

3

A leaf is an item in the tree which does not have any further items hanging off of it, i.e. it's the end of a branch. By definition, files fit this description.

-- folder
   |
   |- folder
   |  |
   |  |- file       <- a leaf
   |  |
   |  -- folder
   |     |
   |     -- file    <- another leaf
   |
   -- file          <- yet another leaf

By setting the RecursiveIteratorIterator to "leaf only" mode, it's going to skip over any item that's not a leaf.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • awesome answer does this mean that `RecursiveDirectoryIterator::SKIP_DOTS` is not needed when using leaf mode? – JasonDavis Jan 11 '12 at 12:46
  • I would think so, but couldn't confirm that off the top of my head. – deceze Jan 11 '12 at 12:47
  • Won't that include empty directories? – Rob Agar Jan 11 '12 at 12:48
  • Empty directories have `.` and `..` entries at least. Actually, I'm not sure how that plays together with the `SKIP_DOTS` directive. But in general, I think directories always have children, even if they're empty. – deceze Jan 11 '12 at 12:49
  • This seems to be confirmed by the documentation for [`RecursiveDirectoryIterator::hasChildren`](http://php.net/manual/en/recursivedirectoryiterator.haschildren.php): "Returns whether the current entry is a directory, but not '.' or '..'" – deceze Jan 11 '12 at 12:51