檔案上傳類 PHP 個人自訂版本

來源:互聯網
上載者:User
View Code

destFolder = $custDir; // 自訂上傳的檔案路徑        $this->upPath = $custPath;        $this->upName = $this->setName ( $custName );        $this->upTypes = $custTypes;        $this->upMaxSize = $custMaxSize;        $this->fileField = ( string ) $fileField;        $this->time = time (); // 初始化主要用來統一檔案名稱和目錄    }        /**     * 重新命名上傳檔案,支援中文名     *     * @param string $custName                 * @return string     */    private function setName($custName) {        return ! empty ( $custName ) ? iconv ( "utf-8", "gbk", $custName ) : date ( 'YmdHis', $this->time ) . mt_rand ( 10, 99 );        /*         * if ($custName == '') { // 如果未設定檔案名稱,則產生一個隨機檔案名稱 $name = date (         * 'YmdHis',$this->time ) . "_" . mt_rand ( 10, 99 ) . '.' . $this->ext;         * // 判斷檔案是否存在,不允許重複檔案 if (file_exists ( $this->savePath . $name )) {         * $name = setSavename ( $saveName ); } } else { $name = $saveName; }         * $this->saveName = $name; }         */    }        private function setPath() {        return (preg_match ( '/\/$/', $this->upPath )) ? $this->upPath : $this->upPath . '/';    }    /**     * 建立目錄     *     * @param string $baseDir                 * @param string $destDir                 */    private function mkDirs($baseDir, $destDir) {        $dirs = $baseDir;        ! is_dir ( $dirs ) && @mkdir ( $dirs, 0777 ); // 原來如果前面的假是正的。後面的語句就執行        if (! empty ( $destDir )) {            $destDirs = explode ( '/', $destDir );            foreach ( $destDirs as $finalDir ) {                ! empty ( $finalDir ) && $dirs .= $finalDir . '/';                ! is_dir ( $dirs ) && @mkdir ( $dirs, 0777 );            }        } else {            $dirs .= date ( 'Ymd', $this->time ) . '/';            ! is_dir ( $dirs ) && @mkdir ( $dirs, 0777 );        }        return $dirs;    }        /**     * 獲得尾碼函數     *     * @param string $fileName                 * @return mixed     */    private function getFileExt($filename) {        $extend = pathinfo ( $filename );        $this->chkFileExt = $extend ['extension'];    }        /**     * 檢測檔案尾碼函數     *     * @return boolean     */    private function checkFileExt() {        if (in_array ( $this->chkFileExt, $this->upTypes )) { // 此處程式有bug            return TRUE;        } else {            $this->upError = 1;            return FALSE;        }    }        /**     * 檢測最大尺寸     *     * @return boolean     */    private function checkFileMaxSize() {        if ($this->chkFileSize > $this->upMaxSize) {            $this->upError = 2;            return FALSE;        }        return TRUE;    }    /*     * (non-PHPdoc) @see Upload::fileUpload()     */    public function fileUpload() {        // 單檔案、多檔案上傳        $keys = array_keys ( $_FILES [$this->fileField] ['name'] );        foreach ( $keys as $key ) {            if (! $_FILES [$this->fileField] ['name'] [$key])                continue;            $sysError = $_FILES [$this->fileField] ['error'] [$key];            switch ($sysError) {                case 1 :                    $this->upError = 3;                    break;                case 2 :                    $this->upError = 4;                    break;                case 3 :                    $this->upError = 5;                    break;                case 4 :                    $this->upError = 6;                    break;                case 5 :                    $this->upError = 7;                    break;            }            $this->chkFileName = iconv ( "utf-8", "gbk", $_FILES [$this->fileField] ['name'] [$key] ); // 迴圈中的檔案名稱            $this->chkFileSize = $_FILES [$this->fileField] ['size'] [$key]; // 迴圈中的檔案大小            $this->getFileExt ( $this->chkFileName );            // 檔案類型檢測            if (! $this->checkFileExt ()) {                return $this->errMsg ();                exit ();            }            // 超過大小            if (! $this->checkFileMaxSize ()) {                return $this->errMsg ();                exit ();            }            if ($sysError == 0 && is_uploaded_file ( $_FILES [$this->fileField] ['tmp_name'] [$key] )) {                // 組裝檔案名稱                /*                 * $upFullPathName = $this->upName . $key . '.' .                 * $this->chkFileExt; // 不允許重複 if (file_exists ( $upFullPathName                 * )) { $this->upFullName = $upFullPathName; }                 */                $this->upFullName = $this->upName . $key . '.' . $this->chkFileExt;                $this->upFullPathName = $this->mkDirs ( $this->destFolder, $this->setPath () ) . $this->upFullName;                if (move_uploaded_file ( $_FILES [$this->fileField] ['tmp_name'] [$key], $this->upFullPathName )) {                    $this->sucssInfo ['name'] = $this->upFullPathName;                    $this->sucssInfo ['size'] = $this->chkFileSize;                    $this->sucssInfo ['info'] = '檔案' . $this->upFullName . '上傳成功!';                }            }        }        return $this->sucssInfo;    }    /*     * (non-PHPdoc) @see Upload::errMsg()     */    public function errMsg() {        $errMsg = array (                0 => '檔案上傳成功!',                1 => '上傳檔案' . $this->chkFileName . '類型錯誤,只支援上傳' . implode ( ',', $this->upTypes ) . '等檔案類型!',                2 => '上傳檔案' . $this->chkFileName . '太大,最大支援' . ceil ( $this->upMaxSize / 1024 ) . 'kb的檔案',                3 => '上傳檔案' . $this->chkFileName . '超過了 php.ini 中 upload_max_filesize 選項限制的值。',                4 => '上傳檔案' . $this->chkFileName . '大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值!',                5 => '檔案' . $this->chkFileName . '只有部分被上傳!',                6 => '沒有檔案被上傳。',                7 => '檔案上傳失敗!'         );        if ($this->upError == 0)            return false;        else            return $errMsg [$this->upError];    }}

發布下自己定義的上傳類!環境新人和高手拍磚!

# custom define page          if( access_has_global_level( config_get( 'manage_site_threshold' ) ) ) {               $t_menu_options[] = '' ) . lang_get( 'custorm_list_link' ) . '';               $t_menu_options[] = '' ) . lang_get( 'ot_link' ) . '';               $t_menu_options[] = '' ) . lang_get( 'payment_link' ) . '';          }          # contact list          if( !current_user_is_anonymous() ) {               $t_menu_options[] = '' ) . lang_get( 'contact_list_link' ) . '';          }

  

  • 聯繫我們

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