I have a site currently to display all directories and subdirectories (on MAC os) So I have the code here
$Directory = new RecursiveDirectoryIterator('/');
$Iterator = new RecursiveIteratorIterator($Directory);
foreach($Iterator as $name => $object){
echo "$name<br/>";
}
so it will echo out all of the real path of the directory (currently working on windows environment)
C:\xampp\apache\bin
C:\xampp\apache\bin\.
C:\xampp\apache\bin\..
C:\xampp\apache\bin\ab.exe
C:\xampp\apache\bin\abs.exe
C:\myfolder\mysubfolder
C:\myfolder\mysubfolder\.
C:\myfolder\mysubfolder\..
C:\myfolder\mysubfolder\anotherfile.php
C:\myfolder\mysubfolder\another_folder
C:\myfolder\mysubfolder\another_folder\.
C:\myfolder\mysubfolder\another_folder\..
C:\myfolder\mysubfolder\another_folder\file.txt
What I want to do is, to display in list so something like:
C:\
xampp\
apache\
bin\
ab.exe
abs.exe
myfolder\
mysubfolder\
anotherfile.php\
another_folder\
file.txt
Is it possible to format the data in array like the one I put above, not the full path? I am not sure how to use 'explode' and to group them into new array...?
HI again, I tried these code:
foreach($objects as $name => $object){
$newname=explode('\\', $name);
print_r($newname);
}
and it's giving me:
array = { [0] => C:\
[1]=>xampp
[2]=>apache
[3]=>bin}
array = { [0] => C:\
[1]=>xampp
[2]=>apache
[3]=>bin
[4]-> . }
array = { [0] => C:\
[1]=>xampp
[2]=>apache
[3]=>bin
[4]=>.
[5]=>. . }
array = { [0] => C:\
[1]=>xampp
[2]=>apache
[3]=>bin
[4]=>ab.exe }
array = { [0] => C:\
[1]=>xampp
[2]=>apache
[3]=>bin
[4]=>abs.exe }
Now, I'm not sure how to loop them so they will echo in group e.g
xampp
apache
bin
ab.exe
abs.exe
thanks
RecursiveIteratorIteratoriterates over the tree as if it's flat, sparing you the information of the depth, it's probably the wrong tool here. You want to manually recursively iterate and keep track of the depth.