一尘不染

PHP读取子目录并遍历文件如何?

php

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

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

阅读 311

收藏
2020-05-26

共1个答案

一尘不染

RecursiveDirectoryIterator与RecursiveIteratorIterator结合使用。

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