The returned object is an iterator of SplFileInfo objects.
La clase RecursiveDirectoryIterator
(PHP 5)
Introducción
La clase RecursiveDirectoryIterator proporciona una interfaz para iterar recursivamente sistema de archivos de directorios.
Sinopsis de la Clase
RecursiveDirectoryIterator
extends
FilesystemIterator
implements
Traversable
,
Iterator
,
SeekableIterator
,
RecursiveIterator
{
/* Métodos */
public __construct
( string
$path
[, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO
] )/* Métodos heredados */
public FilesystemIterator::__construct
( string
}$path
[, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS
] )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
- RecursiveDirectoryIterator::__construct — Construye un RecursiveDirectoryIterator
- RecursiveDirectoryIterator::getChildren — Devuelve un iterador de la entrada actual si se trata de un directorio
- RecursiveDirectoryIterator::getSubPath — Obtener la sub ruta
- RecursiveDirectoryIterator::getSubPathname — Obtiene la sub ruta y nombre de fichero
- RecursiveDirectoryIterator::hasChildren — Devuelve si la entrada actual es un directorio y no '.' o '..'
- RecursiveDirectoryIterator::key — Devuelve la ruta y nombre de fichero del directorio
- RecursiveDirectoryIterator::next — Mover a la siguiente entrada
- RecursiveDirectoryIterator::rewind — Rebobina el directorio hasta el inicio
catinahat at cool dot fr dot nf ¶
5 months 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);
?>
antennen ¶
2 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.
alvaro at demogracia dot com ¶
4 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.
Thriault ¶
3 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.
joelhy ¶
2 years ago
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);
}
?>
megar ¶
3 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";
?>
Justin Deltener ¶
4 years ago
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.
