天天看點

PHP多檔案上傳類--upload.class.phpPHP多檔案一件上傳類–upload.class.php

PHP多檔案一件上傳類–upload.class.php

最近在學習PHP,寫了一個多檔案上傳類練練手,與單檔案相比,主要注意以下幾點:

  • 在表單中,的multiple屬性要用到,這樣可以選擇多個檔案
  • 擷取檔案名稱時,要用$_FILES[]循環讀取上傳,具體實作方式見代碼
  • *貌似可以通過js的var files = doucument.getElementById(‘files’).files,然後通過循環得到上傳各檔案的名稱,傳遞給PHP背景進行操作,但具體實作方式我還不懂。。希望各路大神嘗試一下,給予解答!
  • 檔案類型我預設選的是圖檔,大小預設是3m,大家可以自己根據需求改一改,或者再寫一個set()方法,直接進行設定。
  • 注釋不少,應該很容易看明白

代碼塊

網頁html代碼:

<html>

<head>
    <title>檔案上傳測試</title>
    <script type="text/javascript" src="uploadFile.js"></script>
</head>

<body>

    <!-- <form action="upload.php" method="post" enctype="multipart/form-data"> -->
    <form action="upload.class.php" method="post" enctype="multipart/form-data">
        <label for="file">Filename:</label>
        <input type="file" name="fileNames[]" id="files" multiple="multiple" />

        <br />
        <input type="submit" name="" value="Submit" />
    </form>
</body>

</html>
           

背景的PHP代碼:

<?php
/**
 * 支援多檔案上傳
 * 面向對象
 * jaywong20170910
 */
date_default_timezone_set('Asia/Shanghai');
class uploadFile{

    //成員屬性
    protected $fileNames;
    protected $supportType;
    protected $maxSize;
    protected $filepath;
    protected $fileInfo;
    protected $error = null;
    protected $upFileName;

    //成員屬性初始化
    public function __construct($fileNames='fileNames',
    $supportType = ['image/jpg','image/png','image/gif','image/jpeg','image/tiff'],
    $maxSize = ,$filepath = '/users/jaywong/sites/uploadTest'){
        $this->fileNames = $fileNames;
        $this->supportType = $supportType;
        $this->maxSize = $maxSize;
        $this->filepath = $filepath;
        $this->fileInfo = $_FILES[$this->fileNames];
    }

    //判斷錯誤
    protected function judgeError($i){
        if($this->fileInfo['error'][$i]){
            switch ($fileInfo['error'][$i]){
                case :
                $this->error = 'UPLOAD_ERR_INI_SIZE:上傳的檔案超過了 php.ini 中 upload_max_filesize 選項限制的值';
                return false;
                case :
                $this->error = 'UPLOAD_ERR_FORM_SIZE:上傳檔案的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值。';
                return false;
                case :
                $this->error = 'UPLOAD_ERR_PARTIAL';
                return false;
                case :
                $this->error = 'UPLOAD_ERR_NO_FILE';
                return false;
                case :
                $this->error = 'UPLOAD_ERR_NO_TMP_DIR';
                return false;
                case :
                $this->error = 'UPLOAD_ERR_CANT_WRITE';
                return false;
            }
        }
    }

    //判斷檔案mime類型
    protected function judgeMime($i){
        if(!in_array($this->fileInfo['type'][$i],$this->supportType)){
            $this->error = $this->fileInfo['name'][$i].'不支援檔案類型.<br>';
            return false;
        }
    }

    //判斷檔案是否超過允許最大值
    protected function judgeSize($i){
        if($this->fileInfo['size'][$i]>$this->maxSize){
            $this->error = $fileName.'檔案太大了,超過了3m.<br>';
            return false;
        }
    }

    //建立目錄
    protected function createDir(){
        if(!file_exists($this->filepath)){
            mkdir($this->filepath);
        }
    }

    //給上傳檔案命名
    protected function newName($i){
        $basename = $this->fileInfo['name'][$i];
        $filename = pathinfo($basename,PATHINFO_FILENAME);
        $extention = pathinfo($basename,PATHINFO_EXTENSION);

        $this->upFileName = $filename.date("Y-m-d H:i:s").'.'.$extention;            
    }

    //上傳檔案
    protected function upFile($i){

        $this->newName($i);

        if(is_uploaded_file($this->fileInfo['tmp_name'][$i])){
            move_uploaded_file($this->fileInfo['tmp_name'][$i],$this->filepath.'/'.$this->upFileName);

            echo $this->upFileName."上傳成功.<br>";

            return array('name'=>$this->fileInfo['name'][$i],
                'type'=>$this->fileInfo['type'][$i],'size'=>$this->fileInfo['size'][$i],
                'newname'=>$this->newName($i));
        }
    }

    //循環上傳檔案
    public function loopUpFiles(){

        // $fileInfo = $_FILES[$fileNames];
        $n = count($this->fileInfo['name']);//$_FILES must be usedfor reading files information.

        for($i = ; $i < $n; $i++){
            //判斷是否有錯誤
            $this->judgeError($i);

            //判斷檔案mime類型
            $this->judgeMime($i);

            //判斷檔案大小
            $this->judgeSize($i);

            //建立目錄
            $this->createDir();

            //上傳檔案
            if(is_null($this->error)){
                $this->upFile($i);
            }
        }
    }

    //檢視錯誤接口
    public function getError(){
        return $this->error;
    }

    //清空對象
    function __destruct(){

    }
}

$upFiles = new uploadFile();
$upFiles->loopUpFiles();

?>