在 PHP 中,可以使用 RecursiveDirectoryIterator
和 RecursiveIteratorIterator
来遍历指定目录及其子目录中的所有文件,并获取文件的属性信息(如文件名、路径、大小、修改时间等)。以下是一个完整的示例代码:
示例代码
<?phpfunction getFileProperties($directory) { $files = []; // 检查目录是否存在 if (!is_dir($directory)) { throw new Exception("目录不存在: $directory"); } // 使用递归迭代器遍历目录 $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST ); foreach ($iterator as $file) { // 只处理文件,跳过目录 if ($file->isFile()) { $files[] = [ 'name' => $file->getFilename(), // 文件名 'path' => $file->getPathname(), // 完整路径 'size' => $file->getSize(), // 文件大小(字节) 'extension' => $file->getExtension(), // 文件扩展名 'modified' => date('Y-m-d H:i:s', $file->getMTime()), // 修改时间 'created' => date('Y-m-d H:i:s', $file->getCTime()), // 创建时间 'permissions' => substr(sprintf('%o', $file->getPerms()), -4), // 文件权限 ]; } } return $files;}// 示例:遍历目录并输出文件属性try { $directory = '/path/to/your/directory'; // 替换为你的目录路径 $files = getFileProperties($directory); echo "目录: $directory\n"; echo "文件数量: " . count($files) . "\n\n"; foreach ($files as $file) { echo "文件名: " . $file['name'] . "\n"; echo "路径: " . $file['path'] . "\n"; echo "大小: " . $file['size'] . " 字节\n"; echo "扩展名: " . $file['extension'] . "\n"; echo "修改时间: " . $file['modified'] . "\n"; echo "创建时间: " . $file['created'] . "\n"; echo "权限: " . $file['permissions'] . "\n"; echo "--------------------------\n"; }} catch (Exception $e) { echo "错误: " . $e->getMessage();}?>
代码说明
RecursiveDirectoryIterator
:用于递归遍历目录。
RecursiveDirectoryIterator::SKIP_DOTS
跳过.
和..
目录。RecursiveIteratorIterator
:用于递归遍历目录及其子目录。
RecursiveIteratorIterator::SELF_FIRST
表示先遍历当前目录,再遍历子目录。文件属性:
getFilename()
:获取文件名。getPathname()
:获取文件的完整路径。getSize()
:获取文件大小(字节)。getExtension()
:获取文件扩展名。getMTime()
:获取文件的最后修改时间(时间戳)。getCTime()
:获取文件的创建时间(时间戳)。getPerms()
:获取文件的权限(八进制)。时间格式化:
使用
date()
函数将时间戳格式化为可读的日期时间。权限格式化:
使用
sprintf('%o', $file->getPerms())
将权限转换为八进制,并截取最后 4 位。
示例输出
假设目录结构如下:
/path/to/your/directory ├── file1.txt ├── file2.jpg └── subdir └── file3.log
运行代码后,输出可能如下:
目录: /path/to/your/directory 文件数量: 3 文件名: file1.txt 路径: /path/to/your/directory/file1.txt 大小: 1024 字节 扩展名: txt 修改时间: 2023-10-01 12:34:56 创建时间: 2023-10-01 12:34:56 权限: 0644 -------------------------- 文件名: file2.jpg 路径: /path/to/your/directory/file2.jpg 大小: 2048 字节 扩展名: jpg 修改时间: 2023-10-01 12:35:00 创建时间: 2023-10-01 12:35:00 权限: 0644 -------------------------- 文件名: file3.log 路径: /path/to/your/directory/subdir/file3.log 大小: 512 字节 扩展名: log 修改时间: 2023-10-01 12:36:00 创建时间: 2023-10-01 12:36:00 权限: 0644 --------------------------
注意事项
目录权限:
确保 PHP 脚本对目标目录有读取权限。
符号链接:
如果目录中包含符号链接,可以使用
RecursiveDirectoryIterator::FOLLOW_SYMLINKS
来跟随符号链接。性能:
对于非常大的目录,遍历可能会消耗较多时间和内存。可以考虑分批次处理或使用缓存。
错误处理:
添加适当的错误处理逻辑,确保脚本的健壮性。
通过以上代码,你可以轻松遍历指定目录并获取所有文件的属性信息。
本文关键词: 目录 遍历 指定 存储 所有 文件
希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, LeCMS, ClassCMS, Fastadmin, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。