Reputation: 383
I'm using this code to get a listing of every folders/subfolders of a repertory :
$path = realpath($userdir);
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) as $filename)
{
echo "$filename\n";
}
However it displays something like this, wich add dot or double dots and displays the folder twice :
C:\wamp\www\gg\ftp\repository\user\mister.
C:\wamp\www\gg\ftp\repository\user\mister..
C:\wamp\www\gg\ftp\repository\user\mister\apatik.
C:\wamp\www\gg\ftp\repository\user\mister\apatik..
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv.
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv..
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv\vcxvcx.
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv\vcxvcx..
Instead of something cleaner like this :
C:\wamp\www\gg\ftp\repository\user\mister
C:\wamp\www\gg\ftp\repository\user\mister\apatik
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv
C:\wamp\www\gg\ftp\repository\user\mister\vvxcv\vcxvcx
Is there a way ? I was previously using the function glob
glob($sub . '/*' , GLOB_ONLYDIR);
which was displaying the folder correctly but i couldn't get it to be recursive to display subfolders aswell.
Thanks
Upvotes: 2
Views: 401
Reputation: 4425
you need something like this
set_time_limit(0);
function scanDirectory($sub = ''){
$folders = glob($sub . '/*' , GLOB_ONLYDIR);
foreach($folders as $folder){
echo "$folder<br />";
scanDirectory($folder);
}
}
scanDirectory();
and this will list all folders on current drive
EDIT
$folders = glob($sub . '/*' , GLOB_ONLYDIR);
will get all the folders in specified directory.
foreach($folders as $folder)
will loop over folders
array.
$sub
will be the folder name that will be explored.
so, these are the folders
A B C
$folders
will have like $folders['A', 'B', 'C']
and in loop, each A B and C
will be passed as $sub
to check either it has more folders
in it or not.
Upvotes: 2