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" : "找不到临时文件"
}
}
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
<?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;
}
}
?>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。