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

前端 未结 9 2059
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("<path>/*", GLOB_ONLYDIR);
    

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

        $path = '<path>';
        $folders = [];
    
        foreach (new \DirectoryIterator($path) as $file)
        {
            if (!$file->isDot() && $file->isDir())
            {
                $folders[] = $file;
            }
        }
    
    0 讨论(0)
  • 2020-12-02 14:07

    To determine whether or not you have a folder or file use the functions is_dir() and is_file()

    For example:

    $path = 'extracted/' . $name[0];
    $results = scandir($path);
    
    foreach ($results as $result) {
        if ($result === '.' or $result === '..') continue;
    
        if (is_dir($path . '/' . $result)) {
            //code to use if directory
        }
    }
    
    0 讨论(0)
  • 2020-12-02 14:12

    First off, rmdir() cannot delete a folder with contents. If safe mode is disabled you can use the following.

    exec("rm -rf folder/"); 
    

    Also look at is_dir()/is_file() or even better the PHP SPL.

    0 讨论(0)
提交回复
热议问题