Home > Backend Development > PHP Tutorial > PHP遍历目录下的全部文件

PHP遍历目录下的全部文件

WBOY
Release: 2016-06-23 13:36:54
Original
931 people have browsed it

代码如下:

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);		}	}}
Copy after login

打印结果如下:

文件名:10

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

可以根据需求改动代码。


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template