PHP Doku:: The RecursiveDirectoryIterator class - class.recursivedirectoryiterator.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige GrunderweiterungenStandard PHP Library (SPL)IteratorenThe RecursiveDirectoryIterator class

Ein Service von Reinhard Neidl - Webprogrammierung.





The RecursiveDirectoryIterator class


The RecursiveDirectoryIterator provides an interface for iterating recursively over filesystem directories.


RecursiveDirectoryIterator extends FilesystemIterator implements Traversable , Iterator , SeekableIterator , RecursiveIterator {
/* Methoden */
__construct ( string $path [, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO ] )
object getChildren ( void )
public string getSubPath ( void )
public string getSubPathname ( void )
bool hasChildren ([ bool $allow_links ] )
string key ( void )
void next ( void )
void rewind ( void )
/* Inherits */
FilesystemIterator::__construct ( string $path [, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS ] )
public mixed FilesystemIterator::current ( void )
public int FilesystemIterator::getFlags ( void )
public string FilesystemIterator::key ( void )
public void FilesystemIterator::next ( void )
public void FilesystemIterator::rewind ( void )
public void FilesystemIterator::setFlags ([ int $flags ] )


Version Beschreibung
5.3.0 The FilesystemIterator was introduced as the parent class. Previously, the parent was the DirectoryIterator.
5.3.0 Implements SeekableIterator.
5.2.11, 5.3.1 Added RecursiveDirectoryIterator::FOLLOW_SYMLINKS


5 BenutzerBeiträge:
- Beiträge aktualisieren...
2.01.2011 18:40
If you use RecursiveDirectoryIterator with RecursiveIteratorIterator and run into UnexpectedValueException you may use this little hack to ignore those directories, such as lost+found on linux.

class IgnorantRecursiveDirectoryIterator extends RecursiveDirectoryIterator {
getChildren() {
        try {
        } catch(
UnexpectedValueException $e) {
            return new

Use just like the normal RecursiveDirectoryIterator.
9.04.2010 9:05
If you would like to get, say, all the *.php files in your project folder, recursively, you could use the following:


= new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);


$Regex will contain a single index array for each PHP file.
15.07.2009 10:57
Usage example:
To see all the files, and count the space usage:

=new RecursiveDirectoryIterator("/path/");

foreach (new
RecursiveIteratorIterator($ite) as $filename=>$cur) {
"$filename => $filesize\n";

"Total: $nbfiles files, $bytestotal bytes\n";
Justin Deltener
10.06.2009 18:45
If you don't get a fully recursive listing, remember to check your permissions on the folders. I just spent 2 hours to find out my root folder didn't have +x on it. I did a chmod g+x and now get a full listing. Oddly, I was able to get a listing of files/folders one level UNDER that folder, but nothing beyond that point which was cause for the confusion.
alvaro at demogracia dot com
18.09.2008 17:15
Usage example:


= realpath('/etc');

$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
$objects as $name => $object){


This prints a list of all files and directories under $path (including $path ifself). If you want to omit directories, remove the RecursiveIteratorIterator::SELF_FIRST part.

PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)