Search code examples
phplistgetsubdirectory

PHP Get all subdirectories of a given directory


How can I get all sub-directories of a given directory without files, .(current directory) or ..(parent directory) and then use each directory in a function?


Solution

  • Option 1:

    You can use glob() with the GLOB_ONLYDIR option.

    Option 2:

    Another option is to use array_filter to filter the list of directories. However, note that the code below will skip valid directories with periods in their name like .config.

    $dirs = array_filter(glob('*'), 'is_dir');
    print_r($dirs);