Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list files inside directory of a directory

Tags:

php

For looping all files in a directory, I created this code in php :

$dir = new DirectoryIterator(dirname(__FILE__));
$files = scandir($dir.'/');
foreach($files as $file) 
{
    echo $file;
    echo "\n";
}

However I am not able to list all files inside multiple subdirectory of a directory.

like image 845
Esha Soni Avatar asked Dec 21 '12 16:12

Esha Soni


1 Answers

Use RecursiveDirectoryIterator

<?php

$path = realpath('/etc');

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

?>
like image 194
GBD Avatar answered Sep 23 '22 22:09

GBD