PHP获取目标路径(图片目录)的文件目录的原生代码,写出来后发现还有很大的优化空间,但由于是练手的作品没再深入修改,但这几天都有同学问我这个问题,所以决定开通博客,并在此献丑,若有更优方法,也请各位不吝斧正!

   闲话少说,代码如下:

$val){ if($val == 'folder'){ $newpath = $path.$key."/"; $temps = $this->refiles($newpath); if($temps){ $newtemp = $this->reall($temps,$newpath); $arr[$key] = $newtemp; } else{ $arr[$key] = 'null'; } } } return $arr; } /** * 函数名称:allfiles() * 函数作用:取得指定路径下的所有目录 * 参数说明:$path:指定的路径 * 返 回 值:$result 目录数组 or false */ public function allfiles($path){ $allfiles = $this -> refiles($path); if($allfiles){ $newallfiles = $this -> reall($allfiles,$path); $allfiles =$newallfiles; } return $allfiles; } /** * 函数名称:Dloop() * 函数作用:双层循环取得结构树 * 参数说明:$path:指定的路径 * 返 回 值:$result 目录数组 or false */ public function Dloop($arr,$path,$space=0,$spaces=0){ foreach($arr as $key => $val){ if(is_array($val)){ $spaces += 15; $newpath = $path.$key."/"; $str .= '
'.$key.''.$kb.' '; $str .= $this -> Dloop($val,$newpath,$spaces,$spaces); } else{ //$str .= '这里写入树'; if(stripos($key,'.jpg')||stripos($key,'.jpeg')){ $fn = '
'.$key; } elseif(stripos($key,'.gif')){ $fn = '
'.$key; } elseif(stripos($key,'.png')){ $fn = '
'.$key; } else{ $fn = '
'.$key; } $kb = round(filesize($path.$key)/1024)."k"; $str .= ''.$fn.''.$kb.' '; } } return $str; }}?>