频道栏目
首页 > 资讯 > 文件处理 > 正文

PHP获取目标路径的文件目录

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

闲话少说,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
classfiles{
  protectedfunctionrefiles($path){
    if(file_exists($path)){
      $ref= scandir($path,1);
      foreach($refas$val){
        if($val=='.'||$val=='..'){
          continue;
        }
        $arr[$val] =is_dir($path.$val)?'folder':'img';
      }
    }
    else{
      $arr= false;
    }
    return$arr;
  }
  protectedfunctionreall($arr,$path){
    foreach($arras$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
  */
  publicfunctionallfiles($path){
    $allfiles=$this-> refiles($path);
    if($allfiles){
      $newallfiles=$this-> reall($allfiles,$path);
      $allfiles=$newallfiles;
    }
    return$allfiles;
  }
  /**
  * 函数名称:Dloop()
  * 函数作用:双层循环取得结构树
  * 参数说明:$path:指定的路径
  * 返 回 值:$result 目录数组 or false
  */
  publicfunctionDloop($arr,$path,$space=0,$spaces=0){
    foreach($arras$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">&nbsp;</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">&nbsp;</td></tr>';
      }
    }
  return$str; 
  }
}
?>
<!--
网页测试部分
<table width="100%"border="0"cellpadding="0"cellspacing="0"class="content_tab">
</?php
include("../../include/files.php");
$files=newfiles;
$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>
-->

相关TAG标签
上一篇:PHP写一个函数,能够遍历一个文件夹下的所有文件和子文件夹
下一篇:PHP 读取大文件的X行到Y行的内容
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站