代码如下:

<?php

header("Content-type: text/html; charset=utf8");

/**

* 递归遍历文件

* @param string $path 目录路径

* @param int $deep=0 当前目录的深度

*/

function readDirs($path,$deep=0) {

// 打开一个目录,读取它的内容

$dh = opendir($path);

while (false !== $file = readdir($dh)) {

// 筛选掉./和../

if ($file != "." && $file != "..") {

// 输出文件

$tmpStr = $file.'<br/>';

if ($deep != 0) {

$tmpStr = str_repeat('&nbsp;&nbsp;', $deep*2-1).'--'.$tmpStr;

}

echo $tmpStr;

// 进入递归点和递归出口

$tmpPath = $path . '/' .$file;

if (is_dir($tmpPath)) {

readDirs($tmpPath,$deep+1);

}

}

}

closedir($dh);

}


readDirs('./');

运行截图如下: