PHP Practical Solutions: File System

Author: Zeeshan Arshad     62     05/23/2014

Estimated reading time: 0 minutes, 36 seconds

Recursive all files and count usage space

[codesyntax lang="php"]

$ite=new RecursiveDirectoryIterator("files/");

foreach (new RecursiveIteratorIterator($ite) as $filename=>$cur) {
echo "$filename => $filesize

echo "Total: $nbfiles files, $bytestotal bytesn";


Convert a Nested Directory Tree into a Multidimensional Array

[codesyntax lang="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);




Was this topic helpful?

Subscribe YouTube Channel

Zeeshan Arshad is a self-taught and multi-dimensional person with extraordinary track record of success with international clients and students.

Feel free to contact him for your project, training purpose or guidance.