views:

29

answers:

2

Hi everybody,

i'm using RecursiveIteratorIterator() to explore subfolders of my current path, but in this way all trhe tree is explored, instead i want just the direct childern of my fodler to be explored for files. How can i say to RecursiveIteratorIterator() to not go much further and stop to the first subfodlers of the current folder?

Following Lauri Lehtinen, i've tried this:

    $it = new RecursiveDirectoryIterator("$directory");
    $it->setMaxDepth(1);

i have PHP 5.2.3 but it say me that setMaxDepth is undefined.

Fatal error: Call to undefined method RecursiveDirectoryIterator::setMaxDepth()
+2  A: 

Use the RecursiveIteratorIterator::setMaxDepth method to set your depth to 1 (or as many levels as you wish):

Lauri Lehtinen
A: 

As Lauri said, setting RecursiveIteratorIterator::setMaxDepth to 1 is the best approach.

Have a look at http://www.php.net/manual/en/class.recursiveiteratoriterator.php#91519 for the following example of how to use it:

<?php
$directory = "/tmp/";
$fileSPLObjects =  new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($directory),
                RecursiveIteratorIterator::CHILD_FIRST
            );
try {
    foreach( $fileSPLObjects as $fullFileName => $fileSPLObject ) {
        print $fullFileName . " " . $fileSPLObject->getFilename() . "\n";
    }
}
catch (UnexpectedValueException $e) {
    printf("Directory [%s] contained a directory we can not recurse into", $directory);
}
?>

If you're having problems, be sure you're using PHP 5.1.0 or later.

labratmatt