PHP如何读取子目录和循环文件? [英] PHP read sub-directories and loop through files how to?
本文介绍了PHP如何读取子目录和循环文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在子目录中的所有文件中创建一个循环.你能帮我像这样构造我的代码吗:
I need to create a loop through all files in subdirectories. Can you please help me struct my code like this:
$main = "MainDirectory";
loop through sub-directories {
loop through filels in each sub-directory {
do something with each file
}
};
推荐答案
使用 RecursiveDirectoryIterator 与 RecursiveIteratorIterator 结合使用.
Use RecursiveDirectoryIterator in conjunction with RecursiveIteratorIterator.
$di = new RecursiveDirectoryIterator('path/to/directory');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
echo $filename . ' - ' . $file->getSize() . ' bytes <br/>';
}
这篇关于PHP如何读取子目录和循环文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文