PHP获取目标路径的文件目录
2013-08-01 14:23
423 查看
PHP获取目标路径(图片目录)的文件目录的原生代码,写出来后发现还有很大的优化空间,但由于是练手的作品没再深入修改,但这几天都有同学问我这个问题,所以决定开通博客,并在此献丑,若有更优方法,也请各位不吝斧正!
闲话少说,代码如下:
闲话少说,代码如下:
<?php class files{ protected function refiles($path){ if(file_exists($path)){ $ref = scandir($path,1); foreach($ref as $val){ if($val=='.'||$val=='..'){ continue; } $arr[$val] = is_dir($path.$val)?'folder':'img'; } } else{ $arr = false; } return $arr; } protected function reall($arr,$path){ foreach($arr as $key =>$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 .= '<tr><td class="content_td"><div style="url(.. /images/type.gif) 0 -51px no-repeat; width:17px; height:17px; float:left; margin:3px 3px auto '.$space.'px;"></div>'.$key.'</td><td class="content_td">'.$kb.'</td><td class="content_td"> </td></tr>'; $str .= $this -> Dloop($val,$newpath,$spaces,$spaces); } else{ //$str .= '这里写入树'; if(stripos($key,'.jpg')||stripos($key,'.jpeg')){ $fn = '<div style="url(.. /images/type.gif) no-repeat; width:17px; height:17px; float:left; margin:3px 3px auto '.$space.'px;"></div>'.$key; } elseif(stripos($key,'.gif')){ $fn = '<div style="url(.. /images/type.gif) 0 -17px no-repeat; width:17px; height:17px; float:left; margin:3px 3px auto '.$space.'px;"></div>'.$key; } elseif(stripos($key,'.png')){ $fn = '<div style="url(.. /images/type.gif) 0 -34px no-repeat; width:17px; height:17px; float:left; margin:3px 3px auto '.$space.'px;"></div>'.$key; } else{ $fn = '<div style="url(.. /images/type.gif) 0 -51px no-repeat; width:17px; height:17px; float:left; margin:3px 3px auto '.$space.'px;"></div>'.$key; } $kb = round(filesize($path.$key)/1024)."k"; $str .= '<tr><td class="content_td">'.$fn.'</td><td class="content_td">'.$kb.'</td><td class="content_td"> </td></tr>'; } } return $str; } } ?> <!-- 网页测试部分 <table width="100%" border="0" cellpadding="0" cellspacing="0" class="content_tab"> </?php include ("../../include/files.php"); $files = new files; $path = '../../images/'; $newallf = $files -> allfiles($path); if ($newallf){ $str = $files -> Dloop($newallf,$path); ?> <tr> <td width="60%" class="content_td">目录及图片</td> <td width="20%" class="content_td">文件大小</td> <td width="20%" class="content_td">管理操作</td> </tr> </?php echo $str; } else {echo '<tr align="center"><td class="content_td">暂无数据</td></tr>';} ?> </table> -->
相关文章推荐
- php获取文件所在目录路径信息 计算程序路径
- PHP文件操作之获取目录下文件与计算相对路径的方法
- PHP获取当前文件路径,上层目录路径
- PHP获取文件绝对路径的代码(上一级目录)
- PHP中获取目录及子目录下的所有文件路径和目录路径
- PHP中获取目录及子目录下的所有文件路径和目录路径
- php 递归获取指定目录下,所有子级、孙级文件路径。
- PHP获取当前文件所在目录的父目录的路径(两种方法)
- PHP文件操作之获取目录下文件与计算相对路径的方法
- php获取目录下所有文件路径(递归)
- PHP获取当前文件路径,上层目录路径
- PHP获取文件绝对路径的代码(上一级目录)
- PHP获取 ,根目录,当前文件路径,上层目录路径 ,文件名,扩展名
- php获取一个目录下的所有文件全路径---包括子目录---并存放到一维数组
- 『IOS』IOS获取工程目录下的资源文件的路径
- iOS 获取文件的目录路径的几种方法
- php递归获取目录下所有文件
- JS获取自身所在文件的目录路径
- [php]将树状文件目录数组转换成路径列表形式
- Application.StartupPath获取执行文件路径substring()取特定长度字符串取得根目录