首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 檔案上傳類完整實作與使用指南

     編輯:PHP 綜合知識

簡介

本文提供一個功能完整的 PHP 檔案上傳類 upload.class.php,支援檔案類型限制、大小限制、覆蓋控制、自動重命名等特性,適合快速整合至專案中。

使用範例

<?php
include_once "upload.class.php";

if ($Submit != '') {
    $fileArr['file'] = $file;
    $fileArr['name'] = $file_name;
    $fileArr['size'] = $file_size;
    $fileArr['type'] = $file_type;

    $filetypes = array('gif','jpg','jpeg','png'); // 允許類型
    $savepath = "/usr/htdocs/upload/";            // 儲存目錄
    $maxsize = 0;                                 // 0 = 無限制
    $overwrite = 0;                               // 0 = 禁止覆蓋

    $upload = new upload($fileArr, $file_name, $savepath, $filetypes, $overwrite, $maxsize);
    if (!$upload->run()) {
        echo "上傳失敗:" . $upload->errmsg();
    }
}
?>
<form enctype="multipart/form-data" method="post">
    <input type="file" name="file">
    <input type="submit" name="Submit" value="上傳">
</form>

類別核心功能

方法說明
upload()建構子,初始化檔案資訊、路徑、格式、大小、覆蓋模式
run()執行上傳流程:驗證格式、目錄可寫、檔案存在、大小限制、複製檔案、刪除暫存
validate_format()檢查副檔名是否在允許清單中
get_ext()解析檔案副檔名
set_savename()設定儲存檔名,空值則自動生成 Ymdhis+隨機數.副檔名
destory()刪除暫存檔
errmsg()取得錯誤訊息

錯誤代碼對照

  • 1:不允許上傳該格式檔案
  • 2:目錄不可寫
  • 3:檔案已存在
  • 4:未知錯誤(複製失敗)
  • 5:檔案過大

完整類別代碼

<?php
$UPLOAD_CLASS_ERROR = array(
    1 => '不允許上傳該格式文件',
    2 => '目錄不可寫',
    3 => '文件已存在',
    4 => '不知名錯誤',
    5 => '文件太大'
);

class upload {
    var $file, $file_name, $file_size, $file_type;
    var $savename, $savepath, $fileformat = array();
    var $overwrite = 0, $maxsize = 0, $ext, $errno = 0;

    function upload($fileArr, $savename, $savepath, $fileformat, $overwrite = 0, $maxsize = 0) {
        $this->file = $fileArr['file'];
        $this->file_name = $fileArr['name'];
        $this->file_size = $fileArr['size'];
        $this->file_type = $fileArr['type'];
        $this->get_ext();
        $this->set_savepath($savepath);
        $this->set_fileformat($fileformat);
        $this->set_overwrite($overwrite);
        $this->set_savename($savename);
        $this->set_maxsize($maxsize);
    }

    function run() {
        if (!$this->validate_format()) { $this->errno = 1; return false; }
        if (!@is_writable($this->savepath)) { $this->errno = 2; return false; }
        if ($this->overwrite == 0 && @file_exists($this->savepath.$this->savename)) { $this->errno = 3; return false; }
        if ($this->maxsize != 0 && $this->file_size > $this->maxsize) { $this->errno = 5; return false; }
        if (!@copy($this->file, $this->savepath.$this->savename)) { $this->errno = 4; return false; }
        $this->destory();
        return true;
    }

    function validate_format() {
        if (!is_array($this->fileformat)) return true;
        $ext = strtolower($this->ext);
        foreach ($this->fileformat as $key) {
            if (strtolower($key) == $ext) return true;
        }
        return false;
    }

    function get_ext() {
        $ext = explode(".", $this->file_name);
        $this->ext = $ext[count($ext)-1];
    }

    function set_maxsize($maxsize) { $this->maxsize = $maxsize; }
    function set_overwrite($overwrite) { $this->overwrite = $overwrite; }
    function set_fileformat($fileformat) { $this->fileformat = $fileformat; }
    function set_savepath($savepath) { $this->savepath = $savepath; }

    function set_savename($savename) {
        if ($savename == '') {
            srand((double)microtime()*1000000);
            $rnd = rand(100,999);
            $name = date('Ymdhis') + $rnd;
            $name = $name.".".$this->ext;
        } else $name = $savename;
        $this->savename = $name;
    }

    function del($file) {
        if (!@unlink($file)) { $this->errno = 3; return false; }
        return true;
    }

    function destory() { $this->del($this->file); }

    function errmsg() {
        global $UPLOAD_CLASS_ERROR;
        return $this->errno ? $UPLOAD_CLASS_ERROR[$this->errno] : false;
    }
}
?>

注意事項

  • 確保上傳目錄具備寫入權限
  • PHP 設定 file_uploads=On、upload_max_filesize、post_max_size 需配合調整
  • 生產環境建議加強 MIME 型別驗證、檔案內容掃描
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved