downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

RecursiveDirectoryIterator::__construct> <RecursiveCallbackFilterIterator::hasChildren
[edit] Last updated: Fri, 23 Mar 2012

view this page in

The RecursiveDirectoryIterator class

(PHP 5)

Wstęp

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

Krótki opis klasy

RecursiveDirectoryIterator extends FilesystemIterator implements Traversable , Iterator , SeekableIterator , RecursiveIterator {
/* Metody */
public __construct ( string $path [, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO ] )
public mixed getChildren ( void )
public string getSubPath ( void )
public string getSubPathname ( void )
public bool hasChildren ([ bool $allow_links ] )
public string key ( void )
public void next ( void )
public void rewind ( void )
/* Inherits */
public 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 ] )
}

Rejestr zmian

Wersja Opis
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

Spis treści



add a note add a note User Contributed Notes RecursiveDirectoryIterator
Josh Heidenreich 14-Feb-2012 03:51
The returned object is an iterator of SplFileInfo objects.
joelhy 22-Feb-2011 07:39
Here is how to empty a directory using iterator:
<?php
function empty_dir($dir) {
   
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
                                             
RecursiveIteratorIterator::CHILD_FIRST);
    foreach (
$iterator as $path) {
      if (
$path->isDir()) {
        
rmdir($path->__toString());
      } else {
        
unlink($path->__toString());
      }
    }
//    rmdir($dir);
}
?>
antennen 02-Jan-2011 09: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.

<?php
class IgnorantRecursiveDirectoryIterator extends RecursiveDirectoryIterator {
    function
getChildren() {
        try {
            return
parent::getChildren();
        } catch(
UnexpectedValueException $e) {
            return new
RecursiveArrayIterator(array());
        }
    }
}
?>

Use just like the normal RecursiveDirectoryIterator.
Thriault 09-Apr-2010 12:05
If you would like to get, say, all the *.php files in your project folder, recursively, you could use the following:

<?php

$Directory
= 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.
megar 15-Jul-2009 01:57
Usage example:
To see all the files, and count the space usage:

<?php
$ite
=new RecursiveDirectoryIterator("/path/");

$bytestotal=0;
$nbfiles=0;
foreach (new
RecursiveIteratorIterator($ite) as $filename=>$cur) {
   
$filesize=$cur->getSize();
   
$bytestotal+=$filesize;
   
$nbfiles++;
    echo
"$filename => $filesize\n";
}

$bytestotal=number_format($bytestotal);
echo
"Total: $nbfiles files, $bytestotal bytes\n";
?>
Justin Deltener 10-Jun-2009 09: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-Sep-2008 08:15
Usage example:

<?php

$path
= realpath('/etc');

$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach(
$objects as $name => $object){
    echo
"$name\n";
}

?>

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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites