场景
有一个视频上传的功能,涉及文件视频上传和封面上传
一般开发步骤就是,两个上传方法控制两个上传
利用反射
upload.php
public function file()
{
//接收前端请求的文件数据 $request->files;
$classObj = new ClassArr();
$classStat = $classObj->uploadClass();
$uploadObj = $classObj->initClass($type,$classStat,[$request,$type]);
$filePath = $uploadObj->upload();
if($filePath){
$this->writeJson(200, ['url'=>'http://sew.ttcchome.cn'.$filePath], 'success');
}else{
$this->writeJson(400, [], '上传失败!');
}
}
image.php 控制上传的图片属性
class Image extends Base
{
/**
* @var string
* 上传文件的key
*/
public $fileType = 'image';
public $maxSize = 524288;
public $suffixs = [
'png',
'jpeg'
];
}```
video.php 控制上传的视频属性
```php
class Video extends Base
{
/**
* @var string
* 上传文件的key
*/
public $fileType = 'video';
public $maxSize = 524288;
public $suffixs = [
'mp4',
'x-fly'
];
}
Base.php
class Base
{
/**
* @var string
* 上传文件的key
*/
public $type = '';
public function __construct($request,$type = null)
{
$this->request = $request;
if(is_null($type)){
$files = $request->files;
$this->type = array_keys($files)[0];
}else{
$this->type = $type;
}
}
/**
* @return bool
* @throws \Exception
* 上传文件
*/
public function upload()
{
if($this->type!=$this->fileType){
return false;
}
$files = $this->request->getUploadedFile($this->type);
$fileSize = $files->getSize();
$this->checkSize($fileSize);
$fileName = $files->getClientFilename();
$fileType = $files->getClientMediaType();
$this->checkType($fileType);
$res = $this->getFile($fileName);
$flag = $files->moveTo($res);
if(is_null($flag)){
return $this->file;
}
return false;
}
/**
* @param $fileName
* @return string
* 上传文件
*/
public function getFile($fileName)
{
$file = pathinfo($fileName);
$extension = $file['extension'];
$filePath = '/upload/'.$this->type.'/'.date('Y').'/'.date('m');
$basePath = EASYSWOOLE_ROOT;
$dir = $basePath.$filePath;
if(!$dir){
mkdir($dir,0777,true);
}
$baseName = '/'.Utils::getFileKey($fileName).'.'.$extension;
$this->file = $filePath.$baseName;
return $dir.$baseName;
}
/**
* @param $fileSize
* @return bool
* 判断大小
*/
public function checkSize($fileSize)
{
if(empty($fileSize) || $fileSize>$this->maxSize){
throw new \Exception('上传文件过大');
}
}
/**
* @param $fileType
* @return bool
* @throws \Exception
* 判断上传文件类型
*/
public function checkType($fileType)
{
$fileType = explode('/',$fileType);
$fileType = $fileType[1] ?? '';
if (empty($fileType)){
throw new \Exception('上传'.$this->type.'不合法');
}
if(!in_array($fileType,$this->suffixs)){
throw new \Exception('上传'.$this->type.'不合法');
}
return true;
}```
ClassArr,php
```php
class ClassArr
{
/**
* @return string[]
* 上传类
*/
public function uploadClass()
{
return [
'video'=>'\App\Lib\Video',
'image'=>'\App\Lib\Image'
];
}
/**
* @param $type
* @param $suppotClss
* @param array $argument
* @param bool $meedInstance
* @return bool|mixed|object
* @throws \ReflectionException
* 反射类
*/
public function initClass($type,$suppotClss,$argument=[],$needInstance=true)
{
if(!array_key_exists($type,$suppotClss)){
return false;
}
$className = $suppotClss[$type];
return $needInstance ? (new \ReflectionClass($className))->newInstanceArgs($argument) : $className;
}
}