0

i want to write file scan script

this script must search in lot's of folders and make list from files

i wrote this script , but return not working

function checkdir($dir) {
    $file_array = array();
   if (is_dir($dir)) {
     $objects = scandir($dir);
     foreach ($objects as $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 you can see in else condition i wrote $file_array[] = $dir."/".$object; it's printing value but can not store value into array

4
  • Why don't you use the DirectoryIterator? php.net/manual/de/class.directoryiterator.php Commented May 11, 2015 at 16:47
  • you never store the return value of checkdir() recursive calls, so this function will only ever return the contents of the top-level director you pass in. Everything else is basically useless, since you iterate all the sub-directories then throw away the results. Commented May 11, 2015 at 16:49
  • @MarcB sorry , i can't understand your means , can you post example of this ? i want to return array in this function Commented May 11, 2015 at 16:52
  • you need $results[] = checkdir(...);-type stuff. Commented May 11, 2015 at 16:53

2 Answers 2

1

The recursive call needs to add its results into the 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 as $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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

i've changed it but still not woking
Maybe you didn't change it correctly, I've posted the whole function.
0

How about that :

if ( ! function_exists('glob_recursive')){
    // Does not support flag GLOB_BRACE

    function glob_recursive($pattern, $flags = 0){
        $files = glob($pattern, $flags);

        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir){
            $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
        }

        return $files;
    }
}

?>

This is an exemple from php.net, check "glob".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.