热搜词
发表于 2022-9-1 23:31:39 | 显示全部楼层 |阅读模式
PHP遍历文件夹及子文件夹所有文件
  1. <html>
  2.     <body>
  3.         <?php
  4.             function traverse($path = '.') {
  5.                 $current_dir = opendir($path);    //opendir()返回一个目录句柄,失败返回false
  6.                 while(($file = readdir($current_dir)) !== false) {    //readdir()返回打开目录句柄中的一个条目
  7.                     $sub_dir = $path . DIRECTORY_SEPARATOR . $file;    //构建子目录路径
  8.                     if($file == '.' || $file == '..') {
  9.                         continue;
  10.                     } else if(is_dir($sub_dir)) {    //如果是目录,进行递归
  11.                         echo 'Directory ' . $file . ':<br>';
  12.                         traverse($sub_dir);
  13.                     } else {    //如果是文件,直接输出
  14.                         echo 'File in Directory ' . $path . ': ' . $file . '<br>';
  15.                     }
  16.                 }
  17.             }

  18.             traverse('xxtt');
  19.         ?>
  20.     </body>
  21. </html>
复制代码
页面输出

Directory autoload:
File in Directory d:\www\autoload: MyClass.php
File in Directory d:\www\autoload: MyClass2.php
File in Directory d:\www\autoload: test.php
File in Directory d:\www: changelog.txt
File in Directory d:\www: cryptForm.php
File in Directory d:\www: diffDomain.php
Directory ExamingOnline:
Directory New folder:
File in Directory d:\www: example.php
File in Directory d:\www: example2.php
Directory Excel:
File in Directory d:\www\Excel: oleread.inc
File in Directory d:\www\Excel: oleread.php
File in Directory d:\www\Excel: reader.php
File in Directory d:\www: expert.xls
File in Directory d:\www: expert.xlsx
File in Directory d:\www: index.php
File in Directory d:\www: index2.php
File in Directory d:\www: jxlrwtest.xls
File in Directory d:\www: lcs.php
Directory listFile:
File in Directory d:\www\listFile: listFile.php
File in Directory d:\www: nQueens.php
Directory proxypass:
Directory proxy:
File in Directory d:\www\proxypass\proxy: login.php
File in Directory d:\www\proxypass\proxy: result.php
File in Directory d:\www\proxypass: success.php
File in Directory d:\www: README
File in Directory d:\www: somefile0.php

全部评论0
回复
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|手机版|小黑屋|管理员之家 ( 苏ICP备2023053177号-2 )

GMT+8, 2024-11-24 17:20 , Processed in 0.152724 second(s), 22 queries .

Powered by Discuz! X3.5

Cpoyright © 2001-2024 Discuz! Team