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";
?>
A classe RecursiveDirectoryIterator
Introdução
...
Sinopse da classe
RecursiveDirectoryIterator
RecursiveDirectoryIterator
extends
DirectoryIterator
implements
Traversable
,
Iterator
,
RecursiveIterator
{
/* Métodos */
/* Herança */
}Índice
- RecursiveDirectoryIterator::__construct — Constructs a RecursiveDirectoryIterator
- RecursiveDirectoryIterator::getChildren — Retorna um iterador para o elemento atual se ele for um diretório
- RecursiveDirectoryIterator::getSubPath — Get sub path
- RecursiveDirectoryIterator::getSubPathname — Get sub path and name
- RecursiveDirectoryIterator::hasChildren — Retorna se o elemento atual é ou não um diretório que não seja '.' nem '..'
- RecursiveDirectoryIterator::key — Retorna o caminho e o nome do arquivo do elemento atual do diretório
- RecursiveDirectoryIterator::next — Avança para o próximo elemento
- RecursiveDirectoryIterator::rewind — Recomeça a iteração do diretório
RecursiveDirectoryIterator
megar
15-Jul-2009 08:57
15-Jul-2009 08:57
Justin Deltener
10-Jun-2009 04:45
10-Jun-2009 04: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 03:15
18-Sep-2008 03: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.
