So I want to show whole directory and sub directories.
According PHP Doc,
The RecursiveDirectoryIterator provides an interface for iterating recursively over filesystem directories
Here is my code:
$it = new RecursiveDirectoryIterator("c:\php_win");
foreach ($it as $dir) {
var_dump($dir->getFilename());
}
Above code only shown one level directory, all sub directories not been shown.
I know I have to use RecursiveIteratorIterator to get all sub directories, but i just do not know why I need to use a RecursiveIteratorIterator iterator.
what is “iterating recursively over filesystem directories” mean? any web links which explains why we need a IteratorIterator?
The
RecursiveDirectoryIteratoris a class implementing theRecursiveIteratorinterface.The
RecursiveIteratorIteratoris a class implementing theIteratorinterface.With a
foreachtraversal over it, it will take an object implementingRecursiveIteratorand do a recursive traversal (tree traversal) on it, here in PHP not only rewinding, fetching current, advancing next and checking validity but also checking if children exists and then internally iterating over these children, too.So the
RecursiveIteratorItertoris providing object traversal in linear order while performing a tree traversal over a concreteRecursiveIteratorimplementaion.So the
RecursiveDirectoryIteratoris only the container. You can iterate over it straight away (e.g. with foreach), but that linear order object iteration would not do the tree traversal.That is why the
RecursiveIteratorIteratoris there, it offers that tree traversal.Which is exactly your case: Direct iteration over
RecursiveDirectoryIteratoronly does linear object traversal. Only theRecursiveIteratorIteratorknows how to do tree traversal on objects implementing theRecursiveIteratorinterface. As it implements theIteratorinterface itself, it is then possible to do linear object traversal over the tree traversal.See as well my answer to How does RecursiveIteratorIterator works in php, it covers the directory iterators exemplary to show the different modes of tree traversal that are possible.