function echoLevel($num){ if ($num>0){ echo " "; --$num; echoLevel($num); } } function echoName($rootPath,&$arr,$index,$lastLevel){ //循环结束,退出 if (!isset($arr[$index])){ return false; } echoLevel($lastLevel); //输出文件名称 echo "|-".$arr[$index]."\r\n"; $nowPath=$rootPath.'\\'.$arr[$index]; //下一个文件 ++$index; //如果不是目录,接着读取下一个文件内容 if (is_file($nowPath)){ return echoName($rootPath,$arr,$index,$lastLevel); } if (!is_dir($nowPath)){ echo '这是不可能的!!!'; exit(); } //层级+1 $nowLevel=$lastLevel+1; //如果是目录,进入目录继续查询文件 foreachDir($nowPath,$nowLevel); //接着读取下一个文件内容 return echoName($rootPath,$arr,$index,$lastLevel); } function foreachDir($rootPath,$lastLevel){ //目录判断 if(!is_dir($rootPath)) { echo "目录《".$rootPath."》不存在"; } $dir=scandir($rootPath); //空目录判断 if (count($dir)<3){ // echo strrchr($rootPath,'\\')."\\\r\n"; return; } //输出读取目录内容 echoName($rootPath,$dir,2,$lastLevel); } foreachDir('C:\work\project\biyouxin',0);