views:

303

answers:

3

How can I Get all subdirectories of a given directory (without files and "." or "..") and then use each directory in a function?

Thanks.

+3  A: 

you can use glob() with GLOB_ONLYDIR option

or

$dirs = array_filter(glob('*'), 'is_dir');
print_r( $dirs);
ghostdog74
that gives subdirectories as well?
Gordon
I didn't think it did....
Yacoby
@Yacoby it doesn't
Gordon
+1  A: 

Almost the same as in your previous question:

$iterator = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($yourStartingPath), 
            RecursiveIteratorIterator::SELF_FIRST);

foreach($iterator as $file) {
    if($file->isDir()) {
        echo strtoupper($file->getRealpath()), PHP_EOL;
    }
}

Replace strtoupper with your desired function.

Gordon
nice thanks! one more question: how can I separate only the sub-dir name from the whole path?
Adrian M.
@Adrian See `dirname` http://www.php.net/manual/en/function.dirname.php
Yacoby
@Adrian Please have a look at the API documentation I gave in your other question. `getFilename()` will return only the directory name.
Gordon
A: 

Sry for answering an old post, but here's how you can retrieve only directories with GLOB:

$directories = glob($somePath . '/*' , GLOB_ONLYDIR);
bakkelun