PHP算法——文件夹遍历类

闲着没事,帮朋友写了一个文件夹遍历类,但是写到了一半,他说不用了,所以下边是功能不是很全的代码,但是基本功能已经实现了

View Code
  1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2 <html xmlns="http://www.w3.org/1999/xhtml">
  3 <head>
  4     <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
  5     <title></title>
  6     <meta name="keywords" content=" keywords" />
  7     <meta name="description" content="description" />
  8 </head>
  9 <body>
 10 <pre>
 11     <?PHP
 12 
 13         /*
 14         *
 15         *    fileArray [0][0] 名称    [1] 时间    [2]    类型        [3]    大小        [4]    可预览性(txt、image)
 16         *    dirArray [0][0] 名称    [1] 时间    [2]    类型(目录)    [3]    大小(-)    
 17         *    @todo:以这个文件为基准点,进去遍历实现文件遍历
 18         *
 19         */
 20         class getIndex {
 21             private $currentDir = '';
 22             private $parentDir = '';
 23             private $fileArray = array();
 24             private $dirArray = array();
 25             function __construct($dir = './'){
 26                 $openDir = @opendir($dir);
 27                 while($fileName = readdir($openDir)){
 28                     if($fileName!='.' && $fileName!='..'){
 29                         if(is_file($dir.$fileName)){
 30                             if($fileName == 'index.html' || $fileName == 'index.php'){
 31                                 continue;
 32                             }
 33                             $this->fileArray[][0] = $fileName;
 34                         }elseif(is_dir($dir.'/'.$fileName)){
 35                             $this->dirArray[][0] = $fileName.'/';
 36                         }
 37                     }
 38                 }
 39             }
 40             private function setDirInfo(){
 41                 foreach($this->dirArray as $key=>$value){
 42                     $this->dirArray[$key][1] = date('Y-m-d',filemtime($value[0]));
 43                     $this->dirArray[$key][2] = '目录';
 44                     $this->dirArray[$key][3] = '—';
 45                 }
 46             }
 47             public function getDirInfo(){
 48                 $this->setDirInfo();
 49                 return $this->dirArray;
 50             }
 51             private function setFileInfo(){
 52                 foreach($this->fileArray as $key=>$value){
 53                     $this->fileArray[$key][1] = date('Y-m-d',filemtime($value[0]));
 54                     if(strrpos($value[0],'.')){
 55                         $this->fileArray[$key][2] = substr($value[0],(strrpos($value[0],'.')+1));
 56                     }else {
 57                         $this->fileArray[$key][2] = '—';
 58                     }
 59                     $this->fileArray[$key][3] = $this->getSize($value[0]);
 60                     //@todo:preview 这里获取了文件的预览,可以更改一下
 61                     if($preview = $this->getPreview($value[0])){
 62                         $this->fileArray[$key][4] = $preview;
 63                     }
 64                 }
 65             }
 66             public function getFileInfo(){
 67                 $this->setFileInfo();
 68                 return $this->fileArray;
 69             }
 70             private function getSize($fileName){
 71                 if(!file_exists($fileName)||!is_readable($fileName)){
 72                     return '—';
 73                 }
 74                 $size = filesize($fileName);
 75                 $units=array('B','KB','MB','GB','TB');
 76                 for($i=0;$size>=1024&&$i<4;$i++)
 77                     $size/=1024;
 78                 return round($size,2).$units[$i];
 79             }
 80             /*
 81             *
 82             *    @tip:出于安全考虑,只能预览图片文件及普通文本文件
 83             *
 84             */
 85             static function getPreview($fileName){
 86                 if(!file_exists($fileName) || !is_readable($fileName)){
 87                     return '<script type="text/javascript">alert("文件不存在或者不可读");</script>';
 88                 }
 89                 $imgTypeArray = array('jpg','png','gif','bmp','ico');
 90                 $textTypeArray = array('txt','lrc','sql','xml');
 91                 $type = substr($fileName,(strrpos($fileName,'.')+1));
 92                 $type = trim($type);
 93                 if(in_array(strtolower($type),$imgTypeArray)){
 94                     return '<img src="'.$fileName.'"/>';
 95                 }elseif(in_array(strtolower($type),$textTypeArray)){
 96                     $result = @file_get_contents($fileName);
 97                     if(mb_check_encoding($result,'utf-8')){
 98                         $result = mb_convert_encoding($result,'gbk','utf-8');
 99                     }
100                     return '<pre>'.$result.'</pre>';
101                 }else {
102                     return false;
103                 }
104                 
105             }
106         }
107         $index = new getIndex();
108         print_r($index->getFileInfo());
109 
110     ?>
111 </pre>
112 </body>
113 </html>

 

posted @ 2012-12-01 10:52  小 伍  阅读(544)  评论(0编辑  收藏  举报