Home >Backend Development >PHP Tutorial >PHP遍历目录下的所有文件

PHP遍历目录下的所有文件

WBOY
WBOYOriginal
2016-06-13 12:17:51965browse

PHP遍历目录下的全部文件

代码如下:

function listDir($dir){	if(is_dir($dir)){		if ($dh = opendir($dir)){			while (($file = readdir($dh)) !== false){				if((is_dir($dir."/".$file)) && $file!="." && $file!=".."){					echo "<b><font color="'red'">文件名:</font></b>",$file,"<br><hr>";					listDir($dir."/".$file."/");				}else{					if($file!="." && $file!=".."){						echo $file."<br>";					}				}			}			closedir($dh);		}	}}

打印结果如下:

文件名:10


文件名:5

2012-12-11.xls
2015-02-01.z
2015-03-11.txt
2015-04-01.rar
文件名:8

可以根据需求改动代码。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn