I’m using a function I found on PHP.net that recursively examines a directory and its contents. It creates a nested array for each subdirectory where elements of those arrays correspond to filenames.
It doesn’t keep track of filepaths. That’s what I want. Here’s the code:
function get_directory_tree( $outerDir, $filters = array() ){
$dirs = array_diff(
scandir( $outerDir ),
array_merge( Array( ".", ".." ), $filters )
);
$dir_array = Array();
foreach( $dirs as $d ){
if(is_dir($outerDir."/".$d)){
$dir_array[ $d ] = get_directory_tree( $outerDir."/".$d, $filters );
} else {
$dir_array[ $d ] = $d;
}
}
return $dir_array;
}
And it outputs this for my test case:
Array(
[a] => Array(
[aa] => Array(
[hjkl.txt] => hjkl.txt
)
[asdf.txt] => asdf.txt
)
[b] => Array()
[c] => Array()
)
And this is what I would like it to output:
Array(
[a] => Array(
[aa] => Array(
[hjkl.txt] => 'a/aa/hjkl.txt'
)
[asdf.txt] => 'a/asdf.txt'
)
[b] => Array()
[c] => Array()
)
Change…
to…