# Get all subdirectories of a given directory in PHP

PHPServer Side ProgrammingProgramming

To get the subdirectories present in a directory, the below lines of code can be used −

## Example

Live Demo

<?php
$all_sub_dirs = array_filter(glob('*.*'), 'is_dir'); print_r($all_sub_dirs);
?>

## Output

This will produce the following output. The glob function is used to get all the subdirectories of a specific directory−

Array (
[0] => demo.csv
[1] => mark.php
[2] => contact.txt
[3] => source.txt
)

To get only the directories, the below lines of code can be used−

## Example

<?php
$all_dirs = glob($somePath . '/*' , GLOB_ONLYDIR);
print_r(\$all_dirs);
?>

## Output

This will produce the following output. The glob function is used by specifying that only directories need to be extracted−

Array (
[0] => example
[1] => exam
[2] => log
)
Published on 30-Dec-2019 11:14:19