利用闲暇时间写一个简单的PHP上传类
HTML代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>文件上传</title>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="up"><br>
<button type="submit">提交</button>
</form>
</body>
</html>
php代码
<?php
class Upload
{
protected $path = './uploads/';
protected $allowSuffix = [
'jpg',
'jpeg',
'gif',
'png',
'wbmp'
];
protected $allowMime = [
'image/jpeg',
'image/gif',
'image/wbmp',
'image/png'
];
protected $maxSize = 2000000;
protected $isRandName = true;
protected $preFix = 'xy_';
protected $errorCode = 999;
protected $errorInfo = '文件上传成功';
protected $oldName;
protected $suffix;
protected $size;
protected $mime;
protected $tempName;
protected $newName;
public function __construct($arr = [])
{
foreach ($arr as $key=>$val){
$this->setOption($key,$val);
}
}
protected function setOption($key,$val)
{
$keys = array_keys(get_class_vars(__CLASS__));
if(in_array($key,$keys)){
$this->$key = $val;
}
}
public function uploadFile($key)
{
if (empty($this->path)){
$this->setOption('errorCode',-1);
return false;
}
if (!$this->check()){
$this->setOption('errorCode',-2);
return false;
}
$error = $_FILES[$key]['error'];
if($error){
$this->setOption('errorCode',$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->tempName)){
if(move_uploaded_file($this->tempName,$this->path.$this->newName)){
return $this->path.$this->newName;
}else{
$this->setOption('errorCode',-7);
return false;
}
}else {
$this->setOption('errorCode',-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->tempName = $_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('errorCode',-3);
return false;
}
return true;
}
protected function checkMime()
{
if(!in_array($this->mime,$this->allowMime)){
$this->setOption('errorCode',-4);
return false;
}
return true;
}
protected function checkSuffix()
{
if(!in_array($this->suffix,$this->allowSuffix)){
$this->setOption('errorCode',-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 == 'errorCode'){
return $this->errorCode;
}else if ($name == 'errorInfo'){
return $this->getErrorInfo();
}
}
protected function getErrorInfo()
{
switch ($this->errorCode){
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;
case 1:
$str = '超出php.ini设置大小';
break;
case 2:
$str = '超出html表单的大小';
break;
case 3:
$str = '文件部分上传';
break;
case 4:
$str = '没有文件部上传';
break;
case 6:
$str = '找不到临时文件';
break;
case 7:
$str = '文件写入失败';
break;
case 999:
$str = '文件上传成功';
break;
}
return $str;
}
}
$test = new Upload();
$test->uploadFile('up');
推荐学习地址
http://xiaoyupp.top/index/Muke/index