Using scandir() to find folders in a directory (PHP)

前端 未结 9 2092
Happy的楠姐
Happy的楠姐 2020-12-02 13:11

I am using this peice of code:

$target = \'extracted/\' . $name[0];  
$scan = scandir($target);

To scan the directory of a folder which is

9条回答
  •  情话喂你
    2020-12-02 14:05

    The quick and dirty way:

    $folders = glob("/*", GLOB_ONLYDIR);
    

    A more versatile and object-oriented solution, inspired by earlier answers using DirectoryIterator but slightly more concise and general purpose:

        $path = '';
        $folders = [];
    
        foreach (new \DirectoryIterator($path) as $file)
        {
            if (!$file->isDot() && $file->isDir())
            {
                $folders[] = $file;
            }
        }
    

提交回复
热议问题