作者 邓超

x

  1 +<?php
  2 +
  3 +namespace Controller;
  4 +
  5 +
  6 +/**
  7 + * 上传文件
  8 + * @author:dc
  9 + * @time 2023/4/18 10:31
  10 + * Class Upload
  11 + * @package Controller
  12 + */
  13 +class Upload extends Base {
  14 +
  15 +
  16 + /**
  17 + * 上传图片
  18 + * @return array
  19 + * @throws \Lib\Err
  20 + * @author:dc
  21 + * @time 2023/4/18 10:32
  22 + */
  23 + public function image(){
  24 +
  25 + $files = app()->file('files');
  26 +
  27 + $result = [];
  28 +
  29 + if($files){
  30 + foreach ($files as $file){
  31 + $file->verify([
  32 + 'ext' => ['jpg','png','gif','jpeg'],
  33 + 'size' => 1024*2,
  34 + 'mine' => ['images/jpeg','images/gif','images/png']
  35 + ]);
  36 +
  37 + if($file->move()){
  38 + $result[] = [
  39 + 'name' => $file->name,
  40 + 'saveUrl' => APP_HOST.$file->savePath.$file->saveName
  41 + ];
  42 + }
  43 +
  44 + }
  45 + }
  46 +
  47 + return $result;
  48 +
  49 + }
  50 +
  51 +
  52 +
  53 +
  54 +
  55 +
  56 +
  57 +}
  58 +
  59 +
  60 +
  61 +
  62 +
  63 +
  64 +
  65 +
  66 +
  67 +
  68 +
  69 +
  70 +
  71 +
@@ -132,4 +132,42 @@ class UploadFile @@ -132,4 +132,42 @@ class UploadFile
132 132
133 133
134 134
  135 + /**
  136 + * 验证
  137 + * @param array $rule
  138 + * @param bool $throw
  139 + * @throws Err
  140 + * @author:dc
  141 + * @time 2023/4/18 10:55
  142 + */
  143 + public function verify(array $rule,bool $throw = true){
  144 + $this->lastVerifyError = '';
  145 + // 后缀
  146 + if(!empty($rule['ext'])){
  147 + $rule['ext'] = is_string($rule['ext']) ? [$rule['ext']] : [];
  148 + if (!in_array($this->ext,$rule['ext'])){
  149 + if(!$throw) return false;
  150 + app()->e(['verify_file.ext',['',$this->name,implode('|',$rule['ext'])]],600);
  151 + }
  152 + }
  153 +
  154 + // 类型
  155 + if(!empty($rule['mine'])){
  156 + $rule['mine'] = is_string($rule['mine']) ? [$rule['mine']] : [];
  157 + if (!in_array($this->mime,$rule['mine'])){
  158 + if(!$throw) return false;
  159 + app()->e(['verify_file.mine',['',$this->name,implode('|',$rule['mine'])]],600);
  160 + }
  161 + }
  162 +
  163 + // 大小
  164 + if(!empty($rule['size'])){
  165 + if ($this->size > $rule['size']){
  166 + if(!$throw) return false;
  167 + app()->e(['verify_file.size',['',$this->name,$rule['size']]],600);
  168 + }
  169 + }
  170 + }
  171 +
  172 +
135 } 173 }
@@ -42,6 +42,9 @@ return [ @@ -42,6 +42,9 @@ return [
42 // 开始任务 42 // 开始任务
43 'job/start' => [\Controller\Job::class, 'start'], 43 'job/start' => [\Controller\Job::class, 'start'],
44 44
  45 + // 上传图片
  46 + 'upload/image' => [\Controller\Upload::class, 'image']
  47 +
45 48
46 49
47 ]; 50 ];