0

I want to use a function to recursively scan a folder, and assign the contents of each scan to an array.

It's simple enough to recurse through each successive index in the array using either next() or foreach - but how to dynamically add a layer of depth to the array (without hard coding it into the function) is giving me problems. Here's some pseudo:

function myScanner($start){

    static $files = array();

    $files = scandir($start);
    //do some filtering here to omit unwanted types

    $next = next($files);


    //recurse scan

    //PROBLEM: how to increment position in array to store results
    //$next_position = $files[][][].... ad infinitum

    //myScanner($start.DIRECTORY_SEPARATOR.$next);
}

any ideas?

2 Answers 2

2

Try something like this:

// $array is a pointer to your array
// $start is a directory to start the scan
function myScanner($start, &$array){
  // opening $start directory handle
  $handle = opendir($start);

  // now we try to read the directory contents
  while (false !== ($file = readdir($handle))) {
    // filtering . and .. "folders"
    if ($file != "." && $file != "..") {
      // a variable to test if this file is a directory
      $dirtest = $start . DIRECTORY_SEPARATOR . $file;

      // check it
      if (is_dir($dirtest)) {
        // if it is the directory then run the function again
        // DIRECTORY_SEPARATOR here to not mix files and directories with the same name
        myScanner($dirtest, $array[$file .  DIRECTORY_SEPARATOR]);
      } else {
        // else we just add this file to an array
        $array[$file] = '';
      }
    }
  }

  // closing directory handle
  closedir($handle);
}

// test it
$mytree = array();
myScanner('/var/www', $mytree);

print "<pre>";
print_r($mytree);
print "</pre>";
Sign up to request clarification or add additional context in comments.

3 Comments

try again, I fixed a little :)
that worked perfectly. I didn't think about using references. This line: myScanner($dirtest, $array[$file . DIRECTORY_SEPARATOR]); is the one that stumped me - I didn't see where the associative keys were getting added - until I understood that references initiate variables. I've learnt something valuable today - thankyou!
blogs.techrepublic.com.com/programming-and-development/?p=417 and then there's always SPL... handy in a pinch
0

Try to use this function (and edit it for your demands):

function getDirTree($dir,$p=true) {
    $d = dir($dir);$x=array();
    while (false !== ($r = $d->read())) {
        if($r!="."&&$r!=".."&&(($p==false&&is_dir($dir.$r))||$p==true)) {
            $x[$r] = (is_dir($dir.$r)?array():(is_file($dir.$r)?true:false));
        }
    }

    foreach ($x as $key => $value) {
        if (is_dir($dir.$key."/")) {
            $x[$key] = getDirTree($dir.$key."/",$p);
        }
    }

    ksort($x);
    return $x;
}

It returns sorted array of directories.

1 Comment

many thanks.... ...but without wanting to cause offense - that's a fairly dense function, and I'd like something a little gentler - perhaps with some 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.