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" : "找不到临时文件"
}
}
<?php
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;
}
}
?>