upload 封装类

<?php
/**
* Created by PhpStorm.
* User: shiyongzhe
* Date: 2018/10/29
* Time: 11:46 AM
*/

/**
* 上传类
* Created by PhpStorm.
* User: shiyongzhe
* Date: 2018/10/29
* Time: 11:46 AM
* Class Upload
*/
class Upload_SyzLocal
{
private $path = '/www/wwwroot/ts.jycf.com/addons/ewei_shopv2/upload';
private $allowtype = ['png','jpg','jpeg'];
private $maxsize = 1000000;
private $israndname = true;
private $originName;
private $tmpFileName;
private $fileType; //文件类型(文件后缀)
private $fileSize; //文件大小
private $newFileName;
private $errorNum;
private $errorMess = '';

public function upload($filename)
{
$return = true;
$name = $_FILES[$filename]['name'];
$tmp_name = $_FILES[$filename]['tmp_name'];
$size = $_FILES[$filename]['size'];
$error = $_FILES[$filename]['error'];
if(is_array($filename))
{
//TODO 多图上传
}else{
if($this->setFiles($name,$tmp_name,$size,$error)){
if($this->checkFileSize() || $this->checkFileType())
{
$this->setNewFileName();
if($this->copyFile())
{
return true;
}else{
$return = false;
}
}else{
$return = false;
}
}else{
$return = false;
}
if(!$return)
{
$this->errorMess = $this->getError();
}
return $return;
}
}

/**
* 获取上传后的文件名称
* @param void 没有参数
* @return string 上传后,新文件的名称, 如果是多文件上传返回数组
*/
public function getFileName(){
return $this->newFileName;
}

/**
* 上传失败后,调用该方法则返回,上传出错信息
* @param void 没有参数
* @return string 返回上传文件出错的信息报告,如果是多文件上传返回数组
*/
public function getErrorMsg(){
return $this->errorMess;
}

/**
* (设置出错信息)
* User: shiyongzhe
* Date: 2018/10/29
* Time: 1:52 PM
* @return string
*/
protected function getError()
{
$str = '错误:';
switch ($this->errorNum)
{
case 4: $str .= '文件没有上传';break;
case 3: $str .= '文件只有部分上传';break;
case 2: $str .= '上传文件的大小超过了HTML表单中MAX_FILE_SIZE选项指定的值'; break;
case 1: $str .= '文件超过系统规定值';break;
case -1: $str .= '未经允许类型';break;
case -2: $str .= '文件字节过大';break;
case -3: $str .= '上传失败';break;
case -4: $str .= '建立存放目录失败';break;
case -5: $str .= '必须指定上传路径';break;
default:$str .= '未知错误!';break;
}
return $str;
}

/**
* (赋值文件到指定的目录)
* User: shiyongzhe
* Date: 2018/10/29
* Time: 1:38 PM
* @return bool
*/
protected function copyFile()
{
if(!$this->errorNum)
{
$path = rtrim($this->path,'/') . '/';
$path .= $this->newFileName;
if(@move_upload_file($this->tmpFileName,$path))
{
return true;
}else{
$this->setOption('errorNum',-3);
return false;
}
}else{
return false;
}
}

/**
* (设置心得文件名)
* User: shiyongzhe
* Date: 2018/10/29
* Time: 1:30 PM
*/
protected function setNewFileName()
{
if($this->israndname)
{
$this->setOption('newFileName',$this->proRandName());
}else{
$this->setOption('newFileName',$this->originName);
}
}

/**
* (检查文件格式是否正确)
* User: shiyongzhe
* Date: 2018/10/29
* Time: 1:27 PM
* @return bool
*/
protected function checkFileType()
{
if(in_array(strtolower($this->fileType),$this->allowtype))
{
return true;
}else{
$this->setOption('errorNum',-1);
return false;
}
}
/**
* (检查文件大小)
* User: shiyongzhe
* Date: 2018/10/29
* Time: 1:25 PM
* @return bool
*/
protected function checkFileSize()
{
if($this->fileSize > $this->maxsize)
{
$this->setOption('errorNum',-2);
return false;
}else{
return true;
}
}

/**
* (设置文件相关的内容)
* User: shiyongzhe
* Date: 2018/10/29
* Time: 1:15 PM
* @param string $name
* @param string $tmp_name
* @param int $size
* @param int $error
* @return bool
*/
protected function setFiles($name = '',$tmp_name = '',$size = 0,$error = 0)
{
$this->setOption('errorNum',$error);
if($error)
{
return false;
}
$this->setOption('originName',$name);
$this->setOption('tmpFileName',$tmp_name);
$aryStr = explode('.',$name);
$this->setOption('fileType',strtolower($aryStr[count($aryStr) - 1]));
$this->setOption('fileSize',$size);
return true;
}

/**
* (设置某个属性值)
* User: shiyongzhe
* Date: 2018/10/29
* Time: 1:09 PM
* @param $key
* @param $val
*/
protected function setOption($key,$val)
{
$this->$key = $val;
}

/**
* (随机文件名)
* User: shiyongzhe
* Date: 2018/10/29
* Time: 1:32 PM
* @return string
*/
protected function proRandName()
{
$fileName = date('YmdHis').'_'.rand(1000,9999);
return $fileName . '.' . $this->fileType;
}


}

后期随时修改完善 暂时只写了单个文件上传 多个文件后期加上 这些算是自己的技术积累
原文地址:https://www.cnblogs.com/shiyongzhe/p/9870634.html