php實現熱門檔案上傳類

來源:互聯網
上載者:User

標籤:pre   html   style   格式不正確   限制   code   元素   UI   xpl   

  1 <?php  2 /**  3  * 上傳檔案類  4  * @param _path : 伺服器檔案存放路徑  5  * @param _allowType : 允許上傳的檔案類型和所對應的MIME  6  * @param _file : 上傳的檔案資訊  7  */  8 class Upload{  9  10     private $_path; 11     private $_allowType; 12     private $_file; 13     /** 14      * 建構函式 15      * @param string : 伺服器上存放上傳檔案的路徑 16      */ 17     function __construct( $path = ‘‘ ) 18     { 19         $this->_path = $path; 20         $this->_allowType = array( 21                 // images 22                 ‘bmp‘ => ‘image/x-ms-bmp‘, 23                 ‘jpg‘ => ‘image/jpeg‘, 24                 ‘jpeg‘ => ‘image/jpeg‘, 25                 ‘gif‘ => ‘image/gif‘, 26                 ‘png‘ => ‘image/png‘, 27                 ‘tif‘ => ‘image/tiff‘, 28                 ‘tiff‘ => ‘image/tiff‘, 29                 ‘tga‘ => ‘image/x-targa‘, 30                 ‘psd‘ => ‘image/vnd.adobe.photoshop‘, 31                 //文本 32                 ‘txt‘ => ‘text/plain‘, 33                 ‘php‘ => ‘text/x-php‘, 34                 ‘html‘ => ‘text/html‘, 35                 ‘htm‘ => ‘text/html‘, 36                 ‘js‘ => ‘text/javascript‘, 37                 ‘css‘ => ‘text/css‘, 38                 ‘rtf‘ => ‘text/rtf‘, 39                 ‘rtfd‘ => ‘text/rtfd‘, 40                 ‘py‘ => ‘text/x-python‘, 41                 ‘java‘ => ‘text/x-java-source‘, 42                 ‘rb‘ => ‘text/x-ruby‘, 43                 ‘sh‘ => ‘text/x-shellscript‘, 44                 ‘pl‘ => ‘text/x-perl‘, 45                 ‘sql‘ => ‘text/x-sql‘, 46                 //應用 47                 ‘exe‘ => ‘application/octet-stream‘, 48                 ‘doc‘ => ‘application/vnd.ms-word‘, 49                 ‘docx‘ => ‘application/vnd.ms-word‘, 50                 ‘xls‘ => ‘application/vnd.ms-excel‘, 51                 ‘ppt‘ => ‘application/vnd.ms-powerpoint‘, 52                 ‘pps‘ => ‘application/vnd.ms-powerpoint‘, 53                 ‘pdf‘ => ‘application/pdf‘, 54                 ‘xml‘ => ‘application/xml‘, 55                 //音頻 56                 ‘mp3‘ => ‘audio/mpeg‘, 57                 ‘mid‘ => ‘audio/midi‘, 58                 ‘ogg‘ => ‘audio/ogg‘, 59                 ‘mp4a‘ => ‘audio/mp4‘, 60                 ‘wav‘ => ‘audio/wav‘, 61                 ‘wma‘ => ‘audio/x-ms-wma‘, 62                 //視頻 63                 ‘avi‘ => ‘video/x-msvideo‘, 64                 ‘dv‘ => ‘video/x-dv‘, 65                 ‘mp4‘ => ‘video/mp4‘, 66                 ‘mpeg‘ => ‘video/mpeg‘, 67                 ‘mpg‘ => ‘video/mpeg‘, 68                 ‘mov‘ => ‘video/quicktime‘, 69                 ‘wm‘ => ‘video/x-ms-wmv‘, 70                 ‘flv‘ => ‘video/x-flv‘, 71                 ‘mkv‘ => ‘video/x-matroska‘ 72             ); 73     } 74     /** 75      * 上傳函數 76      * @param  string : 表單元素的name 值 77      * @return [type] 78      */ 79     public function upload( $txtName = ‘‘ ) 80     { 81         $this->_file = $_FILES[$txtName]; 82         if( $this->_file[‘error‘] == 0){ 83             $fileType = end( explode(‘.‘, $this->_file[‘name‘] )); 84             $allowType = array(); 85             foreach( $this->_allowType as $item=>$value ){ 86                 $allowType[] = $item; 87             } 88             if( !in_array($fileType, $allowType)){ 89                 die(‘上傳的檔案格式不正確!‘); 90             }else{ 91                 if(move_uploaded_file($this->file[‘tmp_name‘], ($this->path).$this->file[‘name‘])) 92                     { 93                         echo "<script>alert(‘上傳成功!‘)</script>"; 94                     } 95                 else 96                     { 97                         echo "<script>alert(‘上傳失敗!‘);</script>"; 98                     } 99             }100 101         }else{102             //沒有正確上傳103             switch ($this->file[‘error‘]){104                 case 1:105                     die(‘檔案大小超過系統限制。‘);106                     break;107                 case 2:108                     die(‘檔案大小超過預定義限制。‘);109                     break;110                 case 3:111                     die(‘檔案為完全上傳。‘);112                     break;113                 case 4:114                     die(‘未上傳任何檔案。‘);115                     break;116                 default:117                     die(‘上傳出錯‘);118                     break;119             }120         }121     }122     //end upload123 }

 

php實現熱門檔案上傳類

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.