how to get all subdirectories that has specific folder name in php?

人走茶凉 提交于 2020-02-14 16:12:10

问题


I find out that I can get all subdirectories of the folder with below code in php

$address = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);

and put it in the $address.

How can I add one more criteria and say if the subdirectory has the 'tmp' folder inside it, then put it in the $address ?


回答1:


You can create your own RecursiveFilterIterator

$dir = new RecursiveDirectoryIterator(__DIR__, 
        RecursiveDirectoryIterator::SKIP_DOTS);

$address = new RecursiveIteratorIterator(new TmpRecursiveFilterIterator($dir), 
        RecursiveIteratorIterator::SELF_FIRST, 
        RecursiveIteratorIterator::CATCH_GET_CHILD);


foreach($address as $dir) {
    echo $dir,PHP_EOL;
}

Class Used

class TmpRecursiveFilterIterator extends RecursiveFilterIterator {
    public function accept() {
        $file = $this->current();
        if ($file->isDir()) {
            return is_dir("$file/tmp");
        }
        return false;
    }
}



回答2:


You probably can add the criteria by creating yourself a FilterIterator that checks for a subdirectory. The following usage example demonstrates this to list folders I have under git.

$address is what you have in your question already, the filter is just added around:

$filtered = new SubDirFilter($address, '.git');

foreach ($filtered as $file) {
    echo $filtered->getSubPathname(), "\n";
}

Output:

Artax
CgiHttpKernel
CgiHttpKernel/vendor/silex/silex
...
composer
composer-setup
CVBacklog
...

And what not. This filter-iterator used is relatively straight forward, for each entry it's checked whether it has that subdiretory or not. It is important that you have the FilesystemIterator::SKIP_DOTS enabled for this (which you have) otherwise you will get duplicate results (expressing the same directory):

class SubDirFilter extends FilterIterator
{
    private $subDir;

    public function __construct(Iterator $iterator, $subDir) {
        $this->subDir = $subDir;
        parent::__construct($iterator);
    }

    public function accept() {
        return is_dir($this->current() . "/" . $this->subDir);
    }
}


来源:https://stackoverflow.com/questions/16389012/how-to-get-all-subdirectories-that-has-specific-folder-name-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!