用php實現熱門檔案上傳類的方法

來源:互聯網
上載者:User
<?php/** * 上傳檔案類 * @param _path : 伺服器檔案存放路徑 * @param _allowType : 允許上傳的檔案類型和所對應的MIME * @param _file : 上傳的檔案資訊*/    class Upload {    private $_path;    private $_allowType;    private $_file;    /** * 建構函式 * @param string : 伺服器上存放上傳檔案的路徑*/    function __construct( $path = '' ) {    $this->_path = $path;    $this->_allowType = array( // images 'bmp' => 'image/x-ms-bmp', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'tga' => 'image/x-targa', 'psd' => 'image/vnd.adobe.photoshop', //文本 'txt' => 'text/plain', 'php' => 'text/x-php', 'html' => 'text/html', 'htm' => 'text/html', 'js' => 'text/javascript', 'css' => 'text/css', 'rtf' => 'text/rtf', 'rtfd' => 'text/rtfd', 'py' => 'text/x-python', 'java' => 'text/x-java-source', 'rb' => 'text/x-ruby', 'sh' => 'text/x-shellscript', 'pl' => 'text/x-perl', 'sql' => 'text/x-sql', //應用 'exe' => 'application/octet-stream', 'doc' => 'application/vnd.ms-word', 'docx' => 'application/vnd.ms-word', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', 'pps' => 'application/vnd.ms-powerpoint', 'pdf' => 'application/pdf', 'xml' => 'application/xml', //音頻 'mp3' => 'audio/mpeg', 'mid' => 'audio/midi', 'ogg' => 'audio/ogg', 'mp4a' => 'audio/mp4', 'wav' => 'audio/wav', 'wma' => 'audio/x-ms-wma', //視頻 'avi' => 'video/x-msvideo', 'dv' => 'video/x-dv', 'mp4' => 'video/mp4', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'mov' => 'video/quicktime', 'wm' => 'video/x-ms-wmv', 'flv' => 'video/x-flv', 'mkv' => 'video/x-matroska' );}/** * 上傳函數 * @param string : 表單元素的name 值 * @return [type]*/    public function upload( $txtName = '' ) {    $this->_file = $_FILES[$txtName];    if( $this->_file['error'] == 0) {    $fileType = end( explode('.', $this->_file['name'] ));    $allowType = array();    foreach( $this->_allowType as $item=>$value ) {    $allowType[] = $item;}if( !in_array($fileType, $allowType)) {    die('上傳的檔案格式不正確!');}else {    if(move_uploaded_file($this->file['tmp_name'], ($this->path).$this->file['name'])) {    echo "<script>alert('上傳成功!')</script>";}else {    echo "<script>alert('上傳失敗!');    </script>";}}}else {    //沒有正確上傳 switch ($this->file['error']) {    case 1: die('檔案大小超過系統限制。');    break;    case 2: die('檔案大小超過預定義限制。');    break;    case 3: die('檔案為完全上傳。');    break;    case 4: die('未上傳任何檔案。');    break;    default: die('上傳出錯');    break;}} }//end upload}

聯繫我們

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