|
Posted by Michael Fesser on July 29, 2008, 9:15 pm
Please log in for more thread options .oO(Jeff)
>Michael Fesser wrote:
>> .oO(petersprc)
>>
>>> You can use the opendir and readdir functions to traverse a directory
>>> tree. There's an example in the doc at php.net/opendir.
>>
>> Or use the various iterators of the SPL. They make it quite convenient
>> to loop through a directory and all that's beneath it if necessary.
>
> Having just now looked at this, I can see how this might work (It
>seems to have some of the file system operators I'm familiar with). But
>the documentation is poor and I'm not sure where I would even start.
>
> Do you have any sample code for this?
To conveniently loop through an entire directory tree $path you would
need two iterators, like this:
$path = '/foo/bar/whatever';
$di = new RecursiveDirectoryIterator($path);
foreach (new RecursiveIteratorIterator($di) as $item) {
if ($item->isFile()) {
...
}
}
Of course this doesn't pay attention to the actual directory or nesting
level, it simply loops through _all_ found entries and sub entries.
See <http://www.php.net/~helly/php/ext/spl/classSplFileInfo.html> for
the various methods you can call on such file objects (just scroll down
a bit to "Public Member Functions").
Micha
|