how can i retrieve the full directory tree
using SPL
?
views:
481answers:
2
+3
A:
You can just, or do everythng that you want
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $file)
{
/* @var $file SplFileInfo */
//...
}
nefo_x
2010-03-17 15:28:48
+2
A:
By default, the RecursiveIteratorIterator
will use LEAVES_ONLY
for the second argument to __construct
. This means it will return files only. If you want to include files and directories (at least that's what I'd consider a full directory tree), you'd have to do:
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path),
RecursiveIteratorIterator::SELF_FIRST);
and then you can foreach
over it. If you want to return the directory tree instead of outputting it, you can store it in an array, e.g.
foreach($iterator as $fileObject) {
$files[] = $fileObject;
// or if you only want the filenames
$files[] = $fileObject->getPathname();
}
If you only want directories returned, foreach
over the $iterator
like this:
foreach($iterator as $fileObject) {
if( $fileObject->isDir() ) {
$files[] = $fileObject;
}
}
Gordon
2010-04-16 19:28:36
many thanks @Gordon, very detailed ;)
kmunky
2010-04-18 23:03:39