PHP遍历文件夹及子文件夹所有文件(此外还有飞递归的方法)

 <html>
<body>
<?php
function traverse($path = '.') {
$current_dir = opendir($path); //opendir()返回一个目录句柄,失败返回false
while(($file = readdir($current_dir)) !== false) { //readdir()返回打开目录句柄中的一个条目
$sub_dir = $path . DIRECTORY_SEPARATOR . $file; //构建子目录路径
if($file == '.' || $file == '..') {
continue;
} else if(is_dir($sub_dir)) { //如果是目录,进行递归
echo 'Directory ' . $file . ':<br>';
traverse($sub_dir);
} else { //如果是文件,直接输出
echo 'File in Directory ' . $path . ': ' . $file . '<br>';
}
}
} traverse('xxtt');
?>
</body>
</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
Directory Study:
Directory .idea:
File in Directory d:\www\Study\.idea: .name
File in Directory d:\www\Study\.idea: encodings.xml
File in Directory d:\www\Study\.idea: misc.xml
File in Directory d:\www\Study\.idea: modules.xml
File in Directory d:\www\Study\.idea: Study.iml
File in Directory d:\www\Study\.idea: vcs.xml
File in Directory d:\www\Study\.idea: workspace.xml
File in Directory d:\www\Study: NSquare.php
File in Directory d:\www\Study: regular.php
Directory templates:
File in Directory d:\www\Study\templates: tpl.php
Directory templates_c:
File in Directory d:\www\Study\templates_c: com_tpl.html.php
File in Directory d:\www\Study\templates_c: com_tpl.php.php
File in Directory d:\www\Study: test.php
Directory sub:
File in Directory d:\www\sub: index.php
Directory template:
File in Directory d:\www\template: template.php
Directory templates:
File in Directory d:\www\template\templates: tpl.html
Directory templates_c:
File in Directory d:\www\template\templates_c: com_tpl.html.php
Directory test:
Directory adodb5:

如果要遍历某个目录下的所有文件(包括子目录),最首先想到的思路就是用递归:先处理当前目录,再处理当前目录下的子目录。不用递归可不可以呢?以前学数据结构的时候看到过,递归其实是利用堆栈来实现的,递归的特点就是不断的调用自身,最后一次的调用是最先执行完的,倒数第二次调用是其次执行完的,依次类推,最初的调用是最后执行完的。如果理解了递归的原理,其实就可以把所有用递归的实现转化为非递归的实现。

用非递归方式遍历某个目录下的所有文件,思路主要分三步:

1. 创建一个数组,将要遍历的这个目录放入;(其实就是创建了一个栈)
2. 循环处理这个数组,循环结束的条件是数组为空;
3. 每次循环,处理数组中的一个元素,并将元素删除,如果这个元素是目录,则将目录下所有的子元素加入数组;

按照这种思路写出的代码如下:

/**
* 遍历某个目录下的所有文件
* @param string $dir
*/
function scanAll($dir)
{
$list = array();
$list[] = $dir;
while (count($list) > 0)
{
//弹出数组最后一个元素
$file = array_pop($list);
//处理当前文件
echo $file."\r\n";
//如果是目录
if (is_dir($file))
{
$children = scandir($file);
foreach ($children as $child)
{
if ($child !== '.' && $child !== '..')
{
$list[] = $file.'/'.$child;
}
}
}
}
}

  

上一篇:01Mybatis_课程安排


下一篇:mybatis由浅入深day01_1课程安排_2对原生态jdbc程序中问题总结