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

来源:互联网 发布:python大数据分析 编辑:程序博客网 时间:2024/05/16 10:44

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

/***   获取指定目录下的文件列表*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;}
原创粉丝点击