I'm trying to create a recursive function that returns all of the files grouped by the directory they are in. My file structure is like this
.
|-- Dir A
| +-- file 1
| +-- file 2
|-- Dir B
| +-- file 11
| +-- file 12
and I want the resulting array to be
array[0][0] = Dir A
array[0][1] = file 1
array[0][2] = file 2
array[1][0] = Dir B
array[1][1] = file 11
array[1][2] = file 12
While the echo's I have show that it finds all of those, the final array looks like this:
array[0][0] = Dir A
array[1][0] = Dir B
So the files are not being stored in the array, or maybe overwritten, but I can't find the mistake. Would someone please point it out? My function is below.
function GetAllFiles($dir = '.', $fileArray = '', $idx = -1){
if ($fileArray == '') $fileArray = array();
if(is_dir($dir)) {
if($dh = opendir($dir)){
while($file = readdir($dh)) {
if($file != '.' && $file != '..'){
if(is_dir($dir . $file)){
$idx++;
echo 'A '.$idx . ' - ' .$dir . $file.'<br>';
$fileArray[$idx][] = $dir . $file;
GetAllFiles($dir . $file . '/', $fileArray, $idx);
}else{
echo 'B '.$idx. ' - ' .$dir . $file.'<br>';
$fileArray[$idx][] = $dir . $file;
}
}
}
}
closedir($dh);
}
return $fileArray;
}
getAllFiles()and the output/error your are getting?