0

I am relatively new to PHP and programming in general and I am attempting to browse a directory and find all subdirectories, and all subdirectories of those subdirectories, and so on and so forth. I googled a bit and stumbled upon the concept of recursion, which I immediately knew was what I needed, because conceptually I already realized it but just didn't know whether or not it was possible. With my limited understanding of the PHP language structure (and general programming constructs for that matter) I'm hitting a bit of a wall here. I saw something about those iterators built into the language but I want to understand what's going into it and be able to put it together myself.

I've created a function that, as far as I can tell, should be able to take an array that has been pre-populated with the directories in the root folder (using a previous glob function) and then look into each of those and keep delving deeper until there are no new directories to be found...but that doesn't seem to be what's happening here. It's spitting out an error telling me that the '[]' operator is not supported for strings, but as far as I can tell '$arrayName[$key]' is an array object, not a string. Even if that particular problem is sorted out, could you experts tell me whether this will work, and if not what needs to be done?

$directory_list[0] = "directory1";
$directory_list[1] = "directory2";
$directory_list[2] = "directory3";

function iterate_unknown_directories($arrayName) {
    foreach ($arrayName as $key => $arrayObject) {
        $directories_check = glob($arrayObject."/*",GLOB_ONLYDIR);
        foreach ($directories_check as $directory) {
            if ($directory !== getcwd()) {
                if ($directory !== $arrayObject) {
                    $arrayName[$key][] = $directory;
                }
            }
        }
        if (is_array($arrayName[$key])) {
            iterate_unknown_directories($arrayName[$key]);
        }
        echo $arrayObject;
    }
}

iterate_unknown_directories($directory_list);

I looked through the resources I was able to find, but none of them seemed to give me exactly what I need. Thank you so much for reading this far, and hopefully for lending a hand!

Mr. Lavalamp
  • 1,860
  • 4
  • 17
  • 29
  • possible duplicate of [PHP recursive directory path](http://stackoverflow.com/questions/2398147/php-recursive-directory-path) – zb' Jan 30 '13 at 05:20
  • I think you should start off by just doing it for a single directory, and then once you get that working, you can add the "search a list of directories" part. – Supericy Jan 30 '13 at 05:21

1 Answers1

0

I'm fairly certain you are over complicating the problem. Try taking a look at this:

function iterate_unknown_directories($dirs)
{
    foreach ($dirs as $dir)
    {
        $subDirs = glob($dir . "/*", GLOB_ONLYDIR);

        /* print out all the new directories*/
        foreach ($subDirs as $subDir)
            echo $subDir . "<br>";

        // recursively search the new directories
        iterate_unknown_directories($subDirs);
    }
}

$directories = array(
    'test_dir'
);

iterate_unknown_directories($directories);
Supericy
  • 5,866
  • 1
  • 21
  • 25