PHP如何读取子目录和循环文件?

标签 php loops directory

我需要创建一个循环遍历子目录中的所有文件。你能帮我这样构造我的代码吗:

$main = "MainDirectory";
loop through sub-directories {
    loop through filels in each sub-directory {
        do something with each file
    }
};

最佳答案

使用RecursiveDirectoryIterator与 RecursiveIteratorIterator 结合使用。

$di = new RecursiveDirectoryIterator('path/to/directory');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
    echo $filename . ' - ' . $file->getSize() . ' bytes <br/>';
}

关于PHP如何读取子目录和循环文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2014474/

相关文章:

c++ - 从给定路径的子目录中计算 txt 文件并包含给定的字符串

PHP - 从数组中获取值

PHP队列实现

php - 强制子类覆盖 PHP 中的特定函数

javascript - 显示代码时遇到问题

xml - Kotlin:格式化字符串

php - 选择他们的(timeStamped)生日晚 X 天的用户

Javascript 数组变量在循环中丢失

android - 确定 Assets 文件夹中文件的文件类型

algorithm - 查找目录的大小