UploadService.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2023/4/17
  6. * Time: 11:29
  7. */
  8. namespace app\common\service;
  9. use app\common\behavior\AdminLogBehavior;
  10. use think\Controller;
  11. use think\Db;
  12. class UploadService extends Controller
  13. {
  14. /**
  15. * 通用的上传
  16. * @param $rootpath string 指定上传路径
  17. * @return \think\response\Json
  18. */
  19. public function upload2path($rootpath)
  20. {
  21. if ($this->request->file('file')) {
  22. $file = $this->request->file('file');
  23. } else {
  24. $res['code'] = 1;
  25. $res['msg'] = '没有上传文件';
  26. return json($res);
  27. }
  28. $fileInfo = $file->getInfo();
  29. //获取上传文件名
  30. $saveFileName = $fileInfo['name'];
  31. //文件校验
  32. $web_config = Db::name('webconfig')->where('id', 1)->find();
  33. $info = $file->validate(['size' => $web_config['file_size'] * 1024, 'ext' => $web_config['file_type']])->rule('date')->move($rootpath, $saveFileName, false);
  34. if ($info) {
  35. (new AdminLogBehavior())->updateLastLog('上传文件:' . $rootpath . $info->getSaveName());
  36. $this->success('上传完成', 'index');
  37. } else {
  38. $this->error('上传失败:' . $file->getError());
  39. }
  40. }
  41. }