maxsize 如何设置">
当前位置:   article > 正文

PHP上传图片_$uplode->maxsize 如何设置

$uplode->maxsize 如何设置
uplode.config.json文件内容
{
  "Img" : {
    "maxSize" : 2048000,
    "minSize" : 0 ,
    "type" : ["png", "jpg", "jpeg", "gif", "bmp"]
  },
  "dangerExt" : ["asp","aspx","php","jsp","htm","html"],
  "message" : {
    "SUCCESS" : "上传成功",
    "ERROR_TYPE_NOT_LRGAL" : "文件类型不合法",
    "ERROR_TYPE_NOT_ALLOWED" : "文件类型不允许",
    "ERROR_SIZE_EXCEED" : "文件大小超出网站限制",
    "ERROR_CREATE_DIR" : "目录创建失败",
    "ERROR_DIR_NOT_WRITEABLE" : "目录没有写权限",
    "ERROR_FILE_NOT_FOUND" : "找不到上传文件或上传文件已被损坏",
    "ERROR_FILE_MOVE" : "文件保存时出错",
    "ERROR_TMP_FILE_NOT_FOUND" : "找不到临时文件"
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
<?php
/**
 * Created by ZYS
 * Date: 16-5-20
 * Time: 下午02: 41
 * 自定义上传类
 */
 class Uploader {
   private $file; //上传对象
   private $fileName;  //文件名称(不含扩展名)
   private $fileNameExt;  //文件扩展名
   private $fileSize;  //文件大小
   private $tmp_name;  //临时文件夹
   private $uplodeType; //上传类型
   private $uplodeFileName; //文件上传的名称
   private $uplodeFilePath; //文件上传的全路径
   private $uplodeFolderPath; //文件上传的文件夹路径
   private $config;  //配置信息
   private $success;  //上传状态信息
   private $message;  //上传信息
   private $ROOT_URL = "../"; //项目根目录(相对于本文件的位置)
   // 构造函数
   public function __construct($controlName,$action){
     if (!$_FILES["$controlName"]) {
       $this->success = false;
       $this->message = $this->config["message"]["ERROR_FILE_NOT_FOUND"];
       return;
     }
     $this->file = $_FILES["$controlName"];
     $fileNameAll = explode(".",$this->file["name"]);
     $this->fileNameExt = $fileNameAll[count($fileNameAll)-1];
     array_splice($fileNameAll,count($fileNameAll)-1,1);
     $this->fileName = implode(".",$fileNameAll);
     $this->fileSize = $this->file["size"];
     $this->tmp_name = $this->file["tmp_name"];
     $this->uplodeType = $action;
     $this->config = json_decode(file_get_contents("uplode.config.json"),true);
     $this->UpFile();
   }

   //获取当前上传成功文件的各项信息
    public function ReturnFileInfo()
    {
        return array(
          "success" => $this->success,
          "message" => $this->message,
          "url" => $this->uplodeFilePath ?: "",
          "size" => $this->fileSize ?: 0,
          "type" => $this->fileNameExt ?: ""
        );
    }

   // 文件上传的主方法
   private function UpFile(){
     //检查临时文件
     if(!file_exists($this->tmp_name)){
       $this->success = false;
       $this->message = $this->config["message"]["ERROR_TYPE_NOT_LRGAL"];
       return;
     }
     //检查后缀是否合法
     if($this->CheckFileExt()){
       $this->success = false;
       $this->message = $this->config["message"]["ERROR_TYPE_NOT_LRGAL"];
       return;
     }
     //检查文件类型是否允许上传
     if(!$this->CheckFileType()){
       $this->success = false;
       $this->message = $this->config["message"]["ERROR_TYPE_NOT_ALLOWED"];
       return;
     }
     //检查文件大小
     if(!$this->CheckFileType()){
       $this->success = false;
       $this->message = $this->config["message"]["ERROR_SIZE_EXCEED"];
       return;
     }
     switch ($this->uplodeType) {
       case "Img":
         $this->UpFile_Img();
         break;
     }
   }

   // 图片上传
   private function UpFile_Img(){
     $this->uplodeFileName = date("YmdHis") . mt_rand(1000,9999);
     $this->uplodeFolderPath = $this->ROOT_URL . "UplodeFile/Image/" . date("Y-m-d") . "/";
     //检查并创建文件夹
     if (!$this->CheckAndCreateFolder()) {
       return;
     }
     $this->uplodeFilePath = $this->uplodeFolderPath.$this->uplodeFileName.".".$this->fileNameExt;
     if (!(move_uploaded_file($this->tmp_name,$this->uplodeFilePath) &&
            file_exists($this->uplodeFilePath))) {
       $this->success = false;
       $this->message = $this->config["message"]["ERROR_FILE_MOVE"];
       return;
     } else {
       $this->success = true;
       $this->message = $this->config["message"]["SUCCESS"];
     }
   }

   //检查后缀是否合法
   private function CheckFileExt(){
     return in_array(strtolower($this->fileNameExt),$this->config["dangerExt"]);
   }

   //检查文件类型是否允许上传
   private function CheckFileType(){
     return in_array(strtolower($this->fileNameExt),$this->config[$this->uplodeType]["type"]);
   }

   //检查文件大小
   private function CheckFileSize(){
     return ($this->fileSize > $this->config[$this->uplodeType]["minSize"] &&
            $this->fileSize <= $this->config[$this->uplodeType]["maxSize"]);
   }

   //检查并创建文件夹
   private function CheckAndCreateFolder(){
     if (!(file_exists($this->uplodeFolderPath) ||
            //以最高权限创建多级文件夹,防止中文乱码
            mkdir(iconv("UTF-8", "GBK", $this->uplodeFolderPath), 0777, true))) {
        $this->message = $this->config["message"]["ERROR_CREATE_DIR"];
        return false;
     }
     if (!is_writeable($this->uplodeFolderPath)) {
       $this->message = $this->config["message"]["ERROR_DIR_NOT_WRITEABLE"];
       return false;
     }
     return true;
   }
 }

?>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/997325
推荐阅读
相关标签
  

闽ICP备14008679号