upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
<form action="./Upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="fm"><br />
<input type="submit" value="上传">
</form>
</div>
</body>
</html>
upload.php
<?php
$up = new Upload();
$up->uploadFile('fm');
var_dump($up->errorNumber);
var_dump($up->errorInfo);
class Upload {
protected $path = './upload/' ;
protected $allowSuffix = ['jpg', 'jpeg', 'gif', 'wbmp', 'png'];
protected $allowMime = ['image/jpeg', 'image/gif', 'image/wbmp', 'image/png'];
protected $maxSize = 2000000;
protected $isRandName = true;
protected $prefix = 'up_';
protected $errorNumber;
protected $errorInfo;
protected $oldName;
protected $suffix;
protected $size;
protected $mime;
protected $tmpName;
protected $newName;
public function __construct ($arr = []) {
foreach ($arr as $key => $value) {
$this->setOption($key, $value);
}
}
protected function setOption ($key, $value) {
$keys = array_keys(get_class_vars(__CLASS__));
if (in_array($key, $keys)) {
$this->$key = $value;
}
}
public function uploadFile ($key) {
if (empty($this->path)) {
$this->setOption('errorNumber', -1);
return false;
}
if (!$this->check()) {
var_dump('check');
$this->setOPtion('errorNumber', -2);
return false;
}
$error = $_FILES[$key]['error'];
if ($error) {
$this->setOPtion('errorNumber', $error);
return false;
} else {
$this->getFileInfo($key);
}
if (!$this->checkSize() || !$this->checkMime() || !$this->checkSuffix()) {
return false;
}
$this->newName = $this->createNewName();
if (is_uploaded_file($this->tmpName)) {
if (move_uploaded_file($this->tmpName, $this->path . $this->newName)) {
return $this->path . $this->newName;
} else {
$this->setOPtion('errorNumber', -7);
return false;
}
} else {
$this->setOPtion('errorNumber', -6);
return false;
}
}
protected function check () {
if (!file_exists($this->path) || !is_dir($this->path)) {
return mkdir($this->path, 0777, true);
}
if (!is_writeable($this->path)) {
return chmod($this->path, 0777);
}
return true;
}
protected function getFileInfo ($key) {
$this->oldName = $_FILES[$key]['name'];
$this->mime = $_FILES[$key]['type'];
$this->tmpName = $_FILES[$key]['tmp_name'];
$this->size = $_FILES[$key]['size'];
$this->suffix = pathinfo($this->oldName)['extension'];
}
protected function checkSize () {
if ($this->size > $this->maxSize) {
$this->setOPtion('errorNumber', -3);
return false;
}
return true;
}
protected function checkMime () {
if (!in_array($this->mime, $this->allowMime)) {
$this->setOPtion('errorNumber', -4);
return false;
}
return true;
}
protected function checkSuffix () {
if (!in_array($this->suffix, $this->allowSuffix)) {
$this->setOPtion('errorNumber', -5);
return false;
}
return true;
}
protected function createNewName () {
if ($this->isRandName) {
$name = $this->prefix.uniqid() . '.' . $this->suffix;
} else {
$name = $this->prefix . $this->oldName;
}
return $name;
}
public function __get($name) {
if ($name == 'errorNumber') {
return $this->errorNumber;
} else if ($name == 'errorInfo') {
return $this->getErrorInfo();
}
}
protected function getErrorInfo () {
switch ($this->errorNumber) {
case -1:
$str = '文件路径没有设置';
break;
case -2:
$str = '文件路径不是目录或者没有权限';
break;
case -3:
$str = '文件大小超出指定范围';
break;
case -4:
$str = '文件mime类型不符合';
break;
case -5:
$str = '文件后缀不符合';
break;
case -6:
$str = '不是上传文件';
break;
case -7:
$str = '文件上传失败';
break;
default:
$str = '上传成功,';
break;
}
return $str;
}
}