3', PHPWEB:'php.cn', settings:'settings', FullScreen:'Full Screen', Speed:'Speed', BrainwashingLoop:'Loop', speedNormal:'Normal', click_to_see:'Click to see full code for direct edit', nextSection:'Next Section', Cancel:'Cancel', Confirm:'Confirm', all_lang_string:'zh|en|zh-tw|ja|ko|ms|fr|de', Next:'Next', Previous:'Previous', };
Home > php教程 > php手册 > php 遍历一个文件夹下的所有文件和子文件夹

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

WBOY
Release: 2016-06-06 20:07:54
Original
1419 people have browsed it

练习来着,怕忘记了...如下代码: ?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')
?>
Copy after login
Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template