
推荐操作系统:windows7系统、PHP5.6、DELL G3电脑
1、方法说明
(1)定义函数,判断是否为目录
(2)如果是目录,则打开目录,返回目录句柄
(3)循环从目录句柄中开始读取
(4)判断读取的文件名是否为目录,如果是目录,则开始递归。
2、实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <!DOCTYPE html>
<html>
<head>
<meta charset= "UTF-8" >
<title>Document</title>
</head>
<body>
<?php
$path = "./phpmyadmin" ;
function showAll( $path ){
if ( is_dir ( $path )){
$handle = opendir( $path );
echo "<ul>" ;
while (false !== $file = readdir( $handle )) {
if ( $file == "." || $file == ".." ){
continue ;
}
echo "<li>$file</li>" ;
if ( is_dir ( $path . '/' . $file )){
showAll( $path . '/' . $file );
}
}
echo "</ul>" ;
closedir ( $handle );
}
}
showAll( $path );
?>
</body>
</html>
|
以上就是php递归遍历文件夹的方法,只要对递归的流程有所了解,就可以展开有关的递归练习啦。更多php学习指路:php教程