作者 邓超

x

<?php
namespace Controller;
/**
* 上传文件
* @author:dc
* @time 2023/4/18 10:31
* Class Upload
* @package Controller
*/
class Upload extends Base {
/**
* 上传图片
* @return array
* @throws \Lib\Err
* @author:dc
* @time 2023/4/18 10:32
*/
public function image(){
$files = app()->file('files');
$result = [];
if($files){
foreach ($files as $file){
$file->verify([
'ext' => ['jpg','png','gif','jpeg'],
'size' => 1024*2,
'mine' => ['images/jpeg','images/gif','images/png']
]);
if($file->move()){
$result[] = [
'name' => $file->name,
'saveUrl' => APP_HOST.$file->savePath.$file->saveName
];
}
}
}
return $result;
}
}
... ...
... ... @@ -132,4 +132,42 @@ class UploadFile
/**
* 验证
* @param array $rule
* @param bool $throw
* @throws Err
* @author:dc
* @time 2023/4/18 10:55
*/
public function verify(array $rule,bool $throw = true){
$this->lastVerifyError = '';
// 后缀
if(!empty($rule['ext'])){
$rule['ext'] = is_string($rule['ext']) ? [$rule['ext']] : [];
if (!in_array($this->ext,$rule['ext'])){
if(!$throw) return false;
app()->e(['verify_file.ext',['',$this->name,implode('|',$rule['ext'])]],600);
}
}
// 类型
if(!empty($rule['mine'])){
$rule['mine'] = is_string($rule['mine']) ? [$rule['mine']] : [];
if (!in_array($this->mime,$rule['mine'])){
if(!$throw) return false;
app()->e(['verify_file.mine',['',$this->name,implode('|',$rule['mine'])]],600);
}
}
// 大小
if(!empty($rule['size'])){
if ($this->size > $rule['size']){
if(!$throw) return false;
app()->e(['verify_file.size',['',$this->name,$rule['size']]],600);
}
}
}
}
\ No newline at end of file
... ...
... ... @@ -42,6 +42,9 @@ return [
// 开始任务
'job/start' => [\Controller\Job::class, 'start'],
// 上传图片
'upload/image' => [\Controller\Upload::class, 'image']
];
... ...