12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- /**
- *
- * User: anyluck
- * Date: 2020/4/17
- * Time: 9:56
- */
- namespace app\common\controller;
- use think\facade\Request;
- use lemo\helper\DataHelper;
- class Upfile
- {
- //上传验证规则
- protected $uploadValidate = [
- 'image' => 'filesize:102400|fileExt:jpg,png,gif,jpeg,rar,zip,avi,mp4,rmvb,3gp,flv,mp3,txt,doc,xls,ppt,pdf,xls,docx,xlsx,doc,wmv'
- ];
- protected $imageValidate = [
- 'image' => 'filesize:10240|fileExt:jpg,png,gif,jpeg,bmp,svg,wmv'
- ];
- protected $videoValidate = [
- 'file' => 'filesize:10240|avi,rmvb,3gp,flv,mp4,wmv'
- ];
- protected $voiceValidate = [
- 'file' => 'filesize:2048|mp3,wma,wav,amr,wmv'
- ];
- public function initialize()
- {
- $fileExt = getConfigByCode('upload_file_type');
- $filemax = getConfigByCode('upload_file_max') * 1024;
- $this->uploadValidate = ['image' =>
- 'filesize' . $filemax . '|' . $fileExt,
- ];
- parent::initialize(); // TODO: Change the autogenerated stub
- }
- // TODO 文件类处理
- /**
- * 文件上传
- * @return false|string
- */
- public function Uploads()
- {
- //获取上传文件表单字段名
- $fileKey = array_keys(request()->file());
- for ($i = 0; $i < count($fileKey); $i++) {
- //获取表单上传文件
- $file = request()->file($fileKey[$i]);
- try {
- validate($this->uploadValidate)->check(DataHelper::objToArray($file));
- $savename = \think\facade\Filesystem::disk('public')->putFile('uploads', $file);
- $savename = str_replace('\\', "/", $savename);
- $path[] = '/storage/' . $savename;
- } catch (\think\exception\ValidateException $e) {
- $path = '';
- $error = $e->getMessage();
- }
- }
- if (!empty($path)) {
- $result['code'] = 1;
- //分辨是否截图上传,截图上传只能上传一个,非截图上传可以上传多个
- if (Request::param('responseType') == 'json') {
- $result["url"] = $path[0];
- } else {
- $result["url"] = $path;
- }
- $result['msg'] = lang('upload success');
- return $result;
- } else {
- //上传失败获取错误信息
- $result['url'] = '';
- $result['msg'] = $error;
- $result['code'] = 0;
- return $result;
- }
- }
- }
|