I'm trying to get the absolute path of some files in a folder with PHP. I use 2 recursive functions. the first one returns all the folders and files information in an array. the second one takes this array as an argument and skips the folders and must return the absolute path of the files. but I don't know where I do wrong. the second function just returns a null array.but when I echo the results it works.
here is the code :
function scan($dir){
$files = array();
if(file_exists($dir)){
foreach(scandir($dir) as $f) {
if(!$f || $f[0] == '.') {
continue;
}
if(is_dir($dir . '/' . $f)) {
$files[] = array(
"name" => $f,
"type" => "folder",
"path" => $dir . '/' . $f,
"items" => scan($dir . '/' . $f)
);
}
else {
$files[] = array(
"name" => $f,
"type" => "file",
"path" => $dir . '/' . $f,
"dirpath" => $dir
);
}
}
}
return $files;
}
function get_files($res)
{
$files = array();
foreach ($res as $re)
{
if($re["type"] == "folder")
{
get_files($re["items"]);
}
else
{
$files[] = $re["path"];
}
}
return $files;
}
function print_files($res)
{
$count = 1;
foreach ($res as $re)
{
if($re["type"] == "folder")
{
print_files($re["items"]);
}
else
{
echo($count." - ".$re["path"]."<br>");
$count++;
}
}
}
the third function works but when I try to return those results in an array with the second function it returns a null array. so the problem is my second function. What can I do about it? thank you in advance.