Psycho Posted April 30, 2013 Share Posted April 30, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277454-recursivedirectoryiterator-with-only-folders/ Share on other sites More sharing options...
requinix Posted April 30, 2013 Share Posted April 30, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277454-recursivedirectoryiterator-with-only-folders/#findComment-1427308 Share on other sites More sharing options...
lemmin Posted April 30, 2013 Share Posted April 30, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277454-recursivedirectoryiterator-with-only-folders/#findComment-1427317 Share on other sites More sharing options...
Psycho Posted April 30, 2013 Author Share Posted April 30, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277454-recursivedirectoryiterator-with-only-folders/#findComment-1427381 Share on other sites More sharing options...
lemmin Posted April 30, 2013 Share Posted April 30, 2013 I just did some basic benchmarking and readdir() was always faster than glob(), even when comparing GLOB_ONLYDIR to if ($file != '.' && $file != '..' && is_dir($path)) Quote Link to comment https://forums.phpfreaks.com/topic/277454-recursivedirectoryiterator-with-only-folders/#findComment-1427389 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.