闲着没事,帮朋友写了一个文件夹遍历类,但是写到了一半,他说不用了,所以下边是功能不是很全的代码,但是基本功能已经实现了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 2 3 4 56 7 8 9 10 11 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 ''; 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 '112 113'; 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 '
'.$result.'';101 }else {102 return false;103 }104 105 }106 }107 $index = new getIndex();108 print_r($index->getFileInfo());109 110 ?>111