获取指定目录下的所有文件名和路径(包括子目录)

简介:这是获取指定目录下的所有文件名和路径(包括子目录)的详细页面,介绍了和php,php, 获取目录文件, 子目录文件 获取指定目录下的所有文件名和路径(包括子目录)有关的知识、技巧、经验,和一些php源码等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=344670' scrolling='no'>

获取指定目录下的所有文件名和路径,同时也包括子目录在内的所有文件

	/**
	*   获取指定目录下的文件列表
	*	string $path 指定的目录,默认为当前目录
	*	string $exten 文件扩展名带前面的点(.txt),默认显示全部文件
	*	string $ifchild 是否显示子目录文件列表,默认不显示
	*/
	function openpath($path=".", $exten = '*' ,$ifchild = false){
		$array = array();
		static $file_array=array(); //存放文件名数组
		static $path_array=array(); //存放路径数组(不包括文件名)
		$path = preg_replace('/(.*)([^\/])$/', '$1$2/', $path);
		if(is_dir($path)){  //检查文件目录是否存在
			$H = @ opendir($path);
			while(false !== ($_file=readdir($H))){
				//检索目录
				if(is_dir($path.$_file) && $_file != "." && $_file!=".." && $_file!=="Thumbs.db"){
					if($ifchild){
						openpath($path.$_file, $exten ,$ifchild);
					}
				//检索文件
				}elseif(is_file($path.$_file) && $_file!="." && $_file!=".." && $_file!=="Thumbs.db"){
					//$_file = auto_charset($_file,'utf-8','gbk');
					if($exten == '*'){
						array_push($file_array, $_file);
						array_push($path_array, $path);
					} else {
						if(preg_match('/(.*)'.$exten.'/', '/'.$_file.'/')){
							array_push($file_array, $_file);
							array_push($path_array, $path);
						}
					}
				}
			}
			closedir($H);
		}
		$array['name'] = $file_array;
		$array['path'] = $path_array;
		return $array;
	}	

爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

http://biancheng.dnbcw.info/php/344670.html pageNo:5
原文地址:https://www.cnblogs.com/ooooo/p/2241930.html