php - Undefined variable in returning foreach loop -
i want write file scan script
this script must search in lot's of folders , make list files
i wrote script , return not working
function checkdir($dir) { $file_array = array(); if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects $key => $object) { if ($object != "." && $object != "..") { if (filetype($dir."/".$object) == "dir") { // folder loop , try find files again checkdir($dir."/".$object); //$file_array[] = 'dir'; } else { $file_array[] = $dir."/".$object; //echo $dir."/".$object.'<br>'; } } }// end foreach //reset($objects); } return $file_array; } as can see in else condition wrote $file_array[] = $dir."/".$object; it's printing value can not store value array
the recursive call needs add results array:
$file_array = array_merge($file_array, checkdir($dir."/".$object)); whole function:
function checkdir($dir) { $file_array = array(); if (is_dir($dir)) { $objects = scandir($dir); foreach ($objects $key => $object) { if ($object != "." && $object != "..") { if (filetype($dir."/".$object) == "dir") { // folder loop , try find files again $file_array = array_merge($file_array, checkdir($dir."/".$object)); //$file_array[] = 'dir'; } else { $file_array[] = $dir."/".$object; //echo $dir."/".$object.'<br>'; } } }// end foreach //reset($objects); } return $file_array; }
Comments
Post a Comment