Getting All the Directories in a Directory

Problem

You want to get a list of all the directories with a directory.

Solution

Use the File::directories() method.

$list = File::directories('/test');
echo "Test contains ", count($list), "directories.";

Discussion

The method returns an array of strings.

Each string is the full path to the subdirectories of the directory argument.

If the directory used as the argument contains no subdirectories an empty array is returned.

If the directory argument doesn't exist, an InvalidArgumentException will be thrown.

comments powered by Disqus