Question:
Could you tell me why this code goes into infinite recursion, because of what it eats up all the RAM and stops working?
public function getFiles($dir, $array = []){
if(is_dir($dir)){
$directory = scandir($dir);
foreach($directory as $file){
if($file != "." and $file != ".."){
if(is_dir($dir."/".$file)){
$directory_files = $this->getFiles($dir."/".$file, $array);
$array = array_merge($array, $directory_files);
}
$array[] = $dir."/".$file;
}
}
}
$array[] = $dir;
return $array;
}
Answer:
Corrected your code so it should work ( not tested ).
function getFiles($dir) {
$result = array();
$directory = scandir($dir);
foreach ($directory as $key => $file)
{
if (!in_array($file,array(".","..")))
{
if (is_dir($dir."/".$file))
$result[$file] = getFiles($dir."/".$value);
else
$result[] = $file;
}
}
return $result;
}
var_dump(getFiles("some_dir"));