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

PHP 文件上傳類完整實例:類型/大小驗證、隨機命名、錯誤處理

     編輯:PHP 綜合知識

class_file 類代碼

file_path = $file_array['tmp_name'];
        $this->file_size = $file_array['size'];
        $this->file_type = $file_array['type'];
        $this->save_path = $save_path;
    }

    public function set_allow_type($allow_type) { $this->allow_type = $allow_type; }
    public function set_allow_size($allow_size) { $this->allow_size = $allow_size; }

    // 主入口
    public function uploadfile()
    {
        if (!$this->allow_file_type()) {
            $this->file_name(); // 仍生成文件名供錯誤時參考
        }
        if (move_uploaded_file($this->file_path, $this->save_path . $this->file_name)) {
            return true;
        } else {
            $this->error = "文件上傳失敗";
            return false;
        }
    }

    private function allow_file_type()
    {
        $this->file_name();
        if (in_array($this->mime_type, $this->allow_type)) return true;
        $this->error = "不允許上傳的類型";
        return false;
    }

    public function allow_file_size($size = 100) // 單位 KB?
    {
        if ($this->is_limit_size) {
            $this->set_allow_size($size * 1024); // 轉字節
            if ($this->allow_size >= $this->file_size) return true;
            $this->error = "超過文件上傳大小限制";
        }
        return false;
    }

    public function is_limit_size() { $this->is_limit_size = true; }

    private function file_name()
    {
        $this->mime_type = substr($this->file_type, strpos($this->file_type, '/') + 1);
        if ($this->mime_type == 'pjpeg') $this->mime_type = 'jpg';
        if ($this->mime_type == 'x-ms-wma') $this->mime_type = 'wma';
        if ($this->mime_type == 'x-ms-wmv') $this->mime_type = 'wmv';
        $this->file_name = date("YmdHis") . ".$this->mime_type";
    }

    public function _get_file_name() { return $this->file_name; }
}
?>

使用示例

set_allow_type(['jpg','jpeg','gif']);
$file->is_limit_size();

if (!$file->allow_file_size(500)) { // 500KB
    echo $file->error; exit;
}
if (!$file->allow_file_type()) {
    echo $file->error; exit;
}
if (!$file->uploadfile()) {
    echo $file->error; exit;
}
echo "上傳成功,文件名: " . $file->_get_file_name();
?>

代碼審視與改進建議

問題現狀建議
構造函數名class_file() (PHP4 風格)改為 __construct()
類型驗證依賴 MIME$_FILES['type'] 可偽造上傳後用 finfo_file() 二次驗證
大小單位混淆allow_file_size(500) 內部乘 1024統一用字節,文檔標註單位
錯誤處理公共 $error 字符串拋出異常或返回 Result 對象
文件名衝突時間戳秒級增加 microtime(true) 或隨機串
目錄不存在無自動創建mkdir($save_path, 0755, true)

現代化重構片段

class FileUploader
{
    private $config;
    public function __construct(array $config) { $this->config = $config + ['maxSize'=>2*1024*1024, 'allowTypes'=>[]]; }

    public function upload(array $file): array
    {
        $this->validate($file);
        $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
        $name = bin2hex(random_bytes(8)) . ".$ext";
        $dest = rtrim($this->config['savePath'], '/') . "/$name";
        if (!move_uploaded_file($file['tmp_name'], $dest)) {
            throw new RuntimeException('Move failed');
        }
        return ['path'=>$dest, 'name'=>$name, 'size'=>$file['size']];
    }
    private function validate(array $file) { /* MIME、大小、錯誤碼 */ }
}

總結

本類展示了 PHP 早期面向過程封裝的典型結構:屬性保狀態、Setter 配置、驗證鏈、執行入口。理解其邏輯有助於閱讀老項目代碼;新項目請採用異常驅動、依賴注入、存儲抽象的現代設計。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved