Jump to content

RecursiveDirectoryIterator with only Folders


Psycho

Recommended Posts

The RecursiveIteratorIterator, RecursiveDirectoryIterator, etc. have me going in circles. I'm trying to create a very efficient method of getting a list of all the subfolders in a target folder. Right now, I have a process using glob() with the "GLOB_ONLYDIR" flag that is taking about 1/2 the time of the process I have using the iterator classes (which should be more efficient). I've also run into an issue where "GLOB_ONLYDIR" does not include folders that begin with a period. I believe I may not be using the iterator classes in the most efficient manner for what I need, as I am looping through the results to parse out the files.

 

Here is my current (in progress) code:

 

function getFolderList($startpath)
{
    //Add the target folder to start the list
    $folders = array($startpath);
    //Create iterator for directory recursion
    $dirIterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($startpath), RecursiveIteratorIterator::SELF_FIRST);

    foreach ($dirIterator as $folder)
    {
        //Skip any files in the result
        if(!$folder->isDir()) continue;
        //Add subfolders to the list
        $folders[] = $startpath . DIRECTORY_SEPARATOR . $dirIterator->getSubPathname();
    }

    return $folders;
}

 

As you can see I am usign a foreach() and skipping the results that are files. In the manual it shows that the RecursiveDirectoryIterator class has the method RecursiveDirectoryIterator::getChildren. But, there is no documentation or examples. I would think there is a way to incorporate that into the instantiation of the iterator object so it only has the directories to begin with so I don't need to run a post-process operation to remove the files.

Link to comment
Share on other sites

getChildren() is required as part of the RecursiveIterator interface. If you're using a RecursiveIteratorIterator then you're not supposed to call it directly - that's handled behind the scenes.

 

I don't see a flag to return only directories. Looks like you're on your own. But as you've seen it's only one additional line of code, and there isn't really any way it could happen more efficiently behind the scenes.

Link to comment
Share on other sites

If you're simply looking for efficiency, using opendir() and readdir() will be the fastest. The RecursiveDirectoryIterator is likely to implement these functions anyway, so making your own recursive function will have less overhead.

 

Using readdir() was just marginally slower than using glob(), but I was able to code it to avoid the bug with folders that begin with a period. So, I guess I'll go with that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.