public function check($rule = [])
{
$rule = $rule ?: $this->validate;
if (isset($rule['size']) && !$this->checkSize($rule['size'])) {
$this->error = '上传文件大小不符!';
return false;
}
if (isset($rule['type']) && !$this->checkMime($rule['type'])) {
$this->error = '上传文件MIME类型不允许!';
return false;
}
if (isset($rule['ext']) && !$this->checkExt($rule['ext'])) {
$this->error = '上传文件后缀不允许';
return false;
}
if (!$this->checkImg()){
$this->error = '非法图像文件!';
return false;
}
return true;
}
public function checkExt($ext)
{
if (is_string($ext)) {
$ext = explode(',', $ext);
}
$extension = strtolower(pathinfo($this->getInfo('name'), PATHINFO_EXTENSION));
if (!in_array($extension, $ext)) {
return false;
}
return true;
}
public function checkImg()// 图片检测
{
$extension = strtolower(pathinfo($this->getInfo('name'), PATHINFO_EXTENSION));
if (in_array($extension, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']) && !in_array($this->getImageType($this->filename), [1, 2, 3, 4, 6])) {
return false;
}
return true;
}
protected function getImageType($image)// 图片类型检测
{
if (function_exists('exif_imagetype')) {
return exif_imagetype($image);
} else {
$info = getimagesize($image);
return $info[2];
}
}
public function checkSize($size)
{
if ($this->getSize() > $size) {
return false;
}
return true;
}
public function checkMime($mime)
{
if (is_string($mime)) {
$mime = explode(',', $mime);
}
if (!in_array(strtolower($this->getMime()), $mime)) {
return false;
}
return true;
}
public function move($path, $savename = true, $replace = true)
{
if (!empty($this->info['error'])) {
$this->error($this->info['error']);
return false;
}
if (!$this->isValid()) {
$this->error = '非法上传文件';
return false;
}
if (!$this->check()) {
return false;
}
$path = rtrim($path, DS) . DS;
$saveName = $this->buildSaveName($savename);
$filename = $path . $saveName;
if (false === $this->checkPath(dirname($filename))) {
return false;
}
if (!$replace && is_file($filename)) {
$this->error = '存在同名文件' . $filename;
return false;
}
if ($this->isTest) {
rename($this->filename, $filename);
} elseif (!move_uploaded_file($this->filename, $filename)) {
$this->error = '文件上传保存错误!';
return false;
}
$file = new self($filename);
$file->setSaveName($saveName);
$file->setUploadInfo($this->info);
return $file;
}
protected function buildSaveName($savename)
{
if (true === $savename) {
if ($this->rule instanceof \Closure) {
$savename = call_user_func_array($this->rule, [$this]);
} else {
switch ($this->rule) {
case 'md5':
$md5 = md5_file($this->filename);
$savename = substr($md5, 0, 2) . DS . substr($md5, 2);
break;
case 'sha1':
$sha1 = sha1_file($this->filename);
$savename = substr($sha1, 0, 2) . DS . substr($sha1, 2);
break;
case 'date':
$savename = date('Ymd') . DS . md5(microtime(true));
break;
default:
$savename = call_user_func($this->rule);
}
}
} elseif ('' === $savename) {
$savename = $this->getInfo('name');
}
if (!strpos($savename, '.')) {
$savename .= '.' . pathinfo($this->getInfo('name'), PATHINFO_EXTENSION);
}
return $savename;
}
private function error($errorNo)// 错误信息翻译
{
switch ($errorNo) {
case 1:
case 2:
$this->error = '上传文件大小超过了最大值!';
break;
case 3:
$this->error = '文件只有部分被上传!';
break;
case 4:
$this->error = '没有文件被上传!';
break;
case 6:
$this->error = '找不到临时文件夹!';
break;
case 7:
$this->error = '文件写入失败!';
break;
default:
$this->error = '未知上传错误!';
}
}
public function getError()// 返回错误信息
{
return $this->error;
}
}