using php to list some files in folders
Posted
by
Terix
on Programmers
See other posts from Programmers
or by Terix
Published on 2013-07-03T00:26:04Z
Indexed on
2013/07/03
5:18 UTC
Read the original article
Hit count: 342
I have collected many free themes from around internet. Each of them has a screenshot.jpg or png file on their folder.
I want to scan all the folders for that file, and return the full file path to be used with an img html tag. I am not interested on partial path or folders where there are not screenshots. For example, if my fodler structure is:
./a/b/
./c/d/e/screenshot.jpg
./f/
./g/screenshot.jpg
./h/i/j/k/
./l/m/screenshot.png
./n/o/
./p/screenshot.jpg
I want to get:
./c/d/e/screenshot.jpg
./g/screenshot.jpg
./l/m/screenshot.png
./p/screenshot.jpg
I managed somehow to get a recursive function, but I figured only the way to return an array and then i can't get rid of what I don't need, and I miss png.
Can anyone help me on that? the code I managed to put together is this:
function getDirectoryTree( $outerDir , $x){
$dirs = array_diff( scandir( $outerDir ), Array( ".", ".." ) );
$dir_array = Array();
foreach( $dirs as $d ){
if( is_dir($outerDir."/".$d) ){
$dir_array[ $d ] = getDirectoryTree( $outerDir."/".$d , $x);
}else{
if ($d==x)
$dir_array[ $d ] = $d;
}
}
return $dir_array;
}
$dirlist = getDirectoryTree('.','screenshot.jpg');
print_r($dirlist);
© Programmers or respective owner