PHP读取子目录并循环通过文件如何? [英] PHP read sub-directories and loop through files how to?

查看:94
本文介绍了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屋!

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