*
* Created by PHPStorm.
* Date: 2018/11/11
* Time: 22:01classUploadFile
{*
**/
const UPLOAD_ERROR =[
UPLOAD_ERR_INI_SIZE=> '文件大小超出了PHP.ini当中的upload_max_filesize的值',1)">
UPLOAD_ERR_FORM_SIZE => '文件大小超出了MAX_FILE_SIZE的值',1)">
UPLOAD_ERR_PARTIAL => '文件只有部分被上传',1)">
UPLOAD_ERR_NO_FILE => '没有文件被上传',1)">
UPLOAD_ERR_NO_TMP_DIR => '找不到临时目录',1)">
UPLOAD_ERR_CANT_WRITE => '写入磁盘失败',1)">
UPLOAD_ERR_EXTENSION => '文件上传被扩展阻止',1)">
];
*
* @varprotected $field_name*
* @var string$destination_dir*
* @var array$allow_mime$allow_ext$file_org_name$file_type$file_tmp_name$file_error$file_size$extension$file_new_name*
* @var float|int$allow_size*
* UploadFile constructor.
* @param $keyName
* @param string $destinationDir
* @param array $allowMime
* @param array $allowExt
* @param float|int $allowSizepublic function __construct($keyName,1)">$destinationDir = './uploads',1)">$allowMime = ['image/jpeg','image/gif'],1)">$allowExt = ['gif','jpeg'],1)">)
{
$this->field_name = $keyName$this->destination_dir = $destinationDir$this->allow_mime = $allowMime$this->allow_ext = $allowExt$this->allow_size = *
* @param $destinationDirfunction setDestinationDir(*
* @param $allowMimefunction setAllowMime(*
* @param $allowExtfunction setAllowExt(*
* @param $allowSizefunction setAllowSize(*
* @return boolfunctionupload()
{判断是否为多文件上传
[];is_array($this->field_name]['name'])) {$this->field_name]['name'] ) {;$this->field_name]['type'][];$this->field_name]['tmp_name'][$this->field_name]['error'][$this->field_name]['size'][];
}
}{$files[] = $this->field_name];
}接收$_FILES参数
$this->setFileInfo($key,1)">);
检查错误
$this->checkError(检查MIME类型
$this->checkMime(检查扩展名
$this->checkExt(检查文件大小
$this->checkSize($this->generateNewName(count((array)$this->getError($key)) > 0) {;
}移动文件
$this->moveFile();
}$this->errors) > 0return false;
}*
* @return arraygetErrors()
{return errors;
}*
* @param $key
* @return mixedprotected function getError()
{$this->errors[];
}function setFileInfo($_FILES name type temp_name error size
$this->file_org_name[$file['name'$this->file_type[$file['type'$this->file_tmp_name[$file['tmp_name'$this->file_error[$file['error'$this->file_size[$file['size'];
}*
* @param $key
* @param $errorfunction setError($key][] = ;
}*
* @param $key
* @return boolfunction checkError($this->file_error >UPLOAD_ERR_OK) {file_error) {case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
case UPLOAD_ERR_PARTIAL:
case UPLOAD_ERR_NO_FILE:
case UPLOAD_ERR_NO_TMP_DIR:
case UPLOAD_ERR_CANT_WRITE:
case UPLOAD_ERR_EXTENSION:
$this->setError(file_error]);;
}
}function checkMime($key],1)">allow_mime)) {
文件类型' . $key] . '不被允许!');function checkExt($this->extension[$key] = PATHINFO_EXTENSION);allow_ext)) {文件扩展名' . $key] . '不被允许!'function checkSize($key] > allow_size) {文件大小' . $key] . '超出了限定大小' . allow_size);*
* @param $keyfunction generateNewName($this->file_new_name[function moveFile(destination_dir)) {$this->destination_dir,1)">);
}$newName = rtrim(];$key]) && $newName)) {;
}上传失败!'*
* @return mixedgetFileName()
{file_new_name;
}*
* @return stringgetDestinationDir()
{destination_dir;
}getExtension()
{extension;
}getFileSize()
{file_size;
}
}