Re: RecursiveDirectoryIterator and foreach
| From: | Nathan Rixham | Date: | Tue, 24 Feb 2009 15:38:49 +0000 |
| Subject: | Re: RecursiveDirectoryIterator and foreach | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-288739@lists.php.net to get a copy of this message | ||
Ryan Panning wrote:
I have discovered that when I foreach over a RecursiveDirectoryIterator (see example below) the $item actually turns into a SplFileInfo object. I would expect it to be a RecursiveDirectoryIterator. How do I do a hasChildren() on SplFileInfo?seems like expected functionality to me, you're looping over the contents of the directory and that can only be a file or a directory - thus SplFileInfo seems correct? in short you don't call hasChildren() on SplFileInfo, you call it on RecursiveDirectoryIterator. From the docs: RecursiveDirectoryIterator::hasChildren — Returns whether current entry is a directory and not '.' or '..' RecursiveDirectoryIterator::getChildren — Returns an iterator for the current entry if it is a directory Thus this is how you call getChildren properly: $dir = new RecursiveDirectoryIterator( dirname(dirname(__FILE__)) ); foreach($dir as $splFileInfo) { if( $dir->hasChildren() ) {
$childDir = $dir->getChildren();
echo get_class($childDir) . ' ' . $childDir->getPath() . PHP_EOL;
}
}
many regards,
Nathan