Upload.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace app\api\controller\v1;
  3. use think\Controller;
  4. class Upload extends Controller
  5. {
  6. public function upfile(){
  7. $file = request()->file('uploadFile');
  8. // 移动到框架应用根目录/uploads/ 目录下
  9. $info = $file->validate([
  10. 'size'=>100*1024*1024,
  11. 'ext'=>'jpg,png,jpeg,doc,docx,xls,xlsx,pdf,ppt,pptx,apk,txt',
  12. // 'type'=>'image/jpeg,image/png,image/jpeg,application/vnd.android.package-archive,application/pdf,application/msword,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-powerpoint,text/plain,application/vnd.openxmlformats-officedocument.presentationml.presentation'
  13. ])
  14. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads/files');
  15. if($info){
  16. $img = '/uploads/files/' . $info->getSaveName();
  17. $img = str_replace('\\', '/', $img);
  18. $data = array(
  19. 'status' => 'done',
  20. 'name' => request()->domain(true).$img,
  21. );
  22. }else{
  23. // 上传失败获取错误信息
  24. $data = array(
  25. 'status' => 'error',
  26. 'msg' => $file->getError(),
  27. );
  28. }
  29. throw new \think\exception\HttpResponseException(json($data));
  30. return;
  31. }
  32. }