PHP 5.6.0 released

La clase RecursiveDirectoryIterator

(PHP 5)

Introducción

La clase RecursiveDirectoryIterator proporciona una interfaz para iterar recursivamente directorios del sistema de ficheros.

Sinopsis de la Clase

RecursiveDirectoryIterator extends FilesystemIterator implements SeekableIterator , RecursiveIterator {
/* Métodos */
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 = false ] )
public string key ( void )
public void next ( void )
public void rewind ( void )
/* Métodos heredados */
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 ] )
}

Historial de cambios

Versión Descripción
5.3.0 La clase FilesystemIterator fue introducida como una clase padre. Anteriormente, el padre era la clase DirectoryIterator.
5.3.0 Se implementó SeekableIterator.
5.2.11, 5.3.1 Agregado RecursiveDirectoryIterator::FOLLOW_SYMLINKS

Tabla de contenidos

add a note add a note

User Contributed Notes 10 notes

up
19
Thriault
4 years ago
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.
up
4
sun
6 months ago
Since I continue to run into implementations across the net that are unintentionally running into this trap — beware:

RecursiveDirectoryIterator recurses without limitations into the full filesystem tree.

Do NOT do the following, unless you intentionally want to infinitely recurse without limitations:

<?php
$directory
= new \RecursiveDirectoryIterator($path);
$iterator = new \RecursiveIteratorIterator($directory);
$files = array();
foreach (
$iterator as $info) {
  if (...
custom conditions...) {
   
$files[] = $info->getPathname();
  }
}
?>

1. RecursiveDirectoryIterator is just a RecursiveIterator that recurses into its children, until no more children are found.

2. The instantiation of RecursiveIteratorIterator causes RecursiveDirectoryIterator to *immediately* recurse infinitely into the entire filesystem tree (starting from the given base path).

3. Unnecessary filesystem recursion is slow.  In 90% of all cases, this is not what you want.

Remember this simple rule of thumb:

→ A RecursiveDirectoryIterator must be FILTERED or you have a solid reason for why it shouldn't.

On PHP <5.4, implement the following - your custom conditions move into a proper filter:

<?php
$directory
= new \RecursiveDirectoryIterator($path, \FilesystemIterator::FOLLOW_SYMLINKS);
$filter = new MyRecursiveFilterIterator($directory);
$iterator = new \RecursiveIteratorIterator($filter);
$files = array();
foreach (
$iterator as $info) {
 
$files[] = $info->getPathname();
}

class
MyRecursiveFilterIterator extends \RecursiveFilterIterator {

  public function
accept() {
   
$filename = $this->current()->getFilename();
   
// Skip hidden files and directories.
   
if ($name[0] === '.') {
      return
FALSE;
    }
    if (
$this->isDir()) {
     
// Only recurse into intended subdirectories.
     
return $name === 'wanted_dirname';
    }
    else {
     
// Only consume files of interest.
     
return strpos($name, 'wanted_filename') === 0;
    }
  }

}
?>

On PHP 5.4+, PHP core addressed the slightly cumbersome issue of having to create an entirely new class and you can leverage the new RecursiveCallbackFilterIterator instead:

<?php
$directory
= new \RecursiveDirectoryIterator($path, \FilesystemIterator::FOLLOW_SYMLINKS);
$filter = new \RecursiveCallbackFilterIterator($directory, function ($current, $key, $iterator) {
 
// Skip hidden files and directories.
 
if ($current->getFilename()[0] === '.') {
    return
FALSE;
  }
  if (
$current->isDir()) {
   
// Only recurse into intended subdirectories.
   
return $current->getFilename() === 'wanted_dirname';
  }
  else {
   
// Only consume files of interest.
   
return strpos($current->getFilename(), 'wanted_filename') === 0;
  }
});
$iterator = new \RecursiveIteratorIterator($filter);
$files = array();
foreach (
$iterator as $info) {
 
$files[] = $info->getPathname();
}
?>

Have fun!
up
9
alvaro at demogracia dot com
5 years ago
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.
up
4
antennen
3 years ago
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 new
IgnorantRecursiveDirectoryIterator($this->getPathname());
        } catch(
UnexpectedValueException $e) {
            return new
RecursiveArrayIterator(array());
        }
    }
}
?>

Use just like the normal RecursiveDirectoryIterator.
up
4
Josh Heidenreich
2 years ago
The returned object is an iterator of SplFileInfo objects.
up
3
megar
5 years ago
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";
?>
up
2
catinahat at cool dot fr dot nf
1 year ago
If you need to convert a nested directory tree into a multidimensional array, use this code:

<?php
$ritit
= new RecursiveIteratorIterator(new RecursiveDirectoryIterator($startpath), RecursiveIteratorIterator::CHILD_FIRST);
$r = array();
foreach (
$ritit as $splFileInfo) {
  
$path = $splFileInfo->isDir()
         ? array(
$splFileInfo->getFilename() => array())
         : array(
$splFileInfo->getFilename());

   for (
$depth = $ritit->getDepth() - 1; $depth >= 0; $depth--) {
      
$path = array($ritit->getSubIterator($depth)->current()->getFilename() => $path);
   }
  
$r = array_merge_recursive($r, $path);
}

print_r($r);
?>
up
2
dblanchard1 at bbox dot fr
9 months ago
If you want to copy all files recursively from a source directory to some destination  :

    $directory = new RecursiveDirectoryIterator("./source_path/");

    foreach (new RecursiveIteratorIterator($directory) as $filename=>$current) {
           
            $src = $current->getPathName();
            $dest = "./destination_path/" . $current->getFileName();
           
            echo "copy " .  $src . " => " . $dest  . "\n";
           
            copy($src, $dest);
     }

I hope it can help someone because when I looked for this solution I had to transform another example to get it.
up
0
Edward Rudd
8 months ago
(related to the post about exceptions in getChildren().

instead of subclassing you can simply use the CATCH_GET_CHILD flag for RecursiveIteratorIterator

new RecursiveIteratorIterator($diriter, RecursiveIteratorIterator::CATCH_GET_CHILD);
up
-1
rockerBOO
7 months ago
When looping through the RecursiveDirectoryIterator , the results use  SplFileInfo.
To Top