PHP opendir() to list folders only

Jeff Thomas picture Jeff Thomas · Jun 27, 2011 · Viewed 47.5k times · Source

I would like to use opendir() to list only the folders in a particular folder (i.e. /www/site/). I would like to exclude files from the list as well at the '.' and '..' folders that appear on a linux folder listing. How would I go about doing this?

Answer

StanleyD picture StanleyD · Feb 5, 2013
foreach(glob('directory/*', GLOB_ONLYDIR) as $dir) {
    $dir = str_replace('directory/', '', $dir);
    echo $dir;
}

You can use simply glob with GLOB_ONLYDIR and then filter resulted directories