Recursive Glob

Categories: Archived

While building the upgrade script for tentacle I knew that the file I was going to work with would be a zip, and would contain many subfolders.

PHP has a function called glob that finds files pathnames matching a pattern.

It works really well but only goes one level deep, using the recursive_glob() function lets you return a folders file structure.

function recursive_glob($pattern = '</em>', $flags = 0, $path = '')
{
    $paths = glob($path . '*', GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT);
    $files = glob($path . $pattern, $flags);
    foreach ($paths as $path) {
        $files = array_merge($files, recursive_glob($pattern, $flags, $path));
    }

    return $files;
}

Adam Patterson

Adam Patterson

User Interface Designer & Developer with a background in UX. I have spent 5 years as a professionally certified bicycle mechanic and ride year-round.

I am a husband and father of two, I enjoy photography, music, movies, coffee, and good food.

You can find me on Twitter, Instagram, and YouTube!