Home >php教程 >php手册 >php 遍历一个文件夹下的所有文件和子文件夹

php 遍历一个文件夹下的所有文件和子文件夹

WBOY
WBOYOriginal
2016-06-06 20:07:541394browse

练习来着,怕忘记了...如下代码: ?php header("Content-type:text/html; charset=utf-8;");//获取文件名后缀function getFileExt($filename){return strtolower(strrchr($filename, '.'));}//列出某个文件目录下的指定文件function listFiles($dir, $ext='.

    练习来着,怕忘记了...如下代码:

<?php header("Content-type:text/html; charset=utf-8;");
	//获取文件名后缀
	function getFileExt($filename)
	{
		return strtolower(strrchr($filename, '.'));
	}
	//列出某个文件目录下的指定文件
	function listFiles($dir, $ext='.php')
	{
		if ( !is_dir($dir))
			exit($dir.'不是一个目录');
		$handle = opendir($dir);
		while ( false !== ($file = readdir($handle)))
		{
			if( $file != '.' && $file != '..')
			{
				$path =  $dir.'/'.$file;
				if (is_dir($path))
					listFiles($path);
				else	
				{
					if (getFileExt($file) == $ext)
						echo $path,'<br />';
				}
			}
		}
	}
	listFiles('/var/www')
?>
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