PHP演算法——檔案夾遍曆類

來源:互聯網
上載者:User

閑著沒事,幫朋友寫了一個檔案夾遍曆類,但是寫到了一半,他說不用了,所以下邊是功能不是很全的代碼,但是準系統已經實現了

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>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.