如何获取所有在php中具有特定文件夹名称的子目录? [英] how to get all subdirectories that has specific folder name in php?

查看:220
本文介绍了如何获取所有在php中具有特定文件夹名称的子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我可以使用以下代码在php中获取文件夹的所有子目录

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"
);

并将其放在$ address中。

and put it in the $address.

如何添加一个条件,并说该子目录中是否包含 tmp文件夹,然后将其放入$ 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 ?

推荐答案

您可以创建自己的 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 TmpRecursiveFilterIterator extends RecursiveFilterIterator {
    public function accept() {
        $file = $this->current();
        if ($file->isDir()) {
            return is_dir("$file/tmp");
        }
        return false;
    }
}

这篇关于如何获取所有在php中具有特定文件夹名称的子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆