layui+php文件上传

单独的文件上传

html文件




    
    upload模块快速使用
    


php文件

fetch();
    }
    public  function  upload(){
        $file = request()->file('file');
              $info = $file->move('public/upload/');
              if($info) {
                  $data['code'] = 0;
                  $data['msg'] = $info->getSaveName();
                  $data['type'] = $info->getExtension();//获取图片的类型
                  if($data['type'] == 'jpg' || $data['type'] == 'gif' || $data['type'] == 'png' || $data['type'] == 'jpeg'){
                      Db::name('image')->insert([
                          'imageurl' => 'http://www.turingwebsite.com/public/upload/'.$data['msg'],
                          'create_time' => time()
                      ]);
                  }
              }else{
                  $data['code'] = 1;
                  $data['msg'] = $file->getError();
              }
              return json($data);
    }
}

文件和表单一起上传

html文件




    
    upload模块快速使用
    


php文件

fetch();
    }
    public  function  upload(){
        $file = request()->file('file');
        $info = $file->move('public/upload/');
        if($info) {
            $data['code'] = 0;
            $data['msg'] = 'http://www.turingwebsite.com/public/upload/'.$info->getSaveName();
            Db::name('image')->insert([
                'imageurl' => $data['msg'],
                'create_time' => time()
            ]);
        }else{
            $data['code'] = 1;
            $data['msg'] = $file->getError();
        }
        return json($data);

    }
    public function  upload2(){
        if ($this->request->isPost()) {
            $Name=input('post.name');
            $hidden=input('post.hidden');
            $Name=input('post.name');
                Db::name('test')->insert([
                    'content' => $Name,
                    'imageurl' => $hidden,
                    'create_time' => time()
                ]);
            $data=array(
                'code' => 0,
                'msg' =>'新增成功'
            );
            return json($data);
        }else{
            $data=array(
                'code' => 1,
                'msg' =>'请求方式错误'
            );
            return json($data);
        }
    }
}

thinkphp6+layui多图上传【异步提交】

sortable.min.js
链接: https://pan.baidu.com/s/1jga1qcyCcLCrLPwsCdDt6w 提取码: u2x3 复制这段内容后打开百度网盘手机App,操作更方便哦

前端示例




    layui ueditor
    
    
    
    
    



php代码

public function upload(Request $request)
   {
       $file = $request->file('file');
       try {
           Validate(['imageFile' => [
               'filesize' => 410241024,
               'fileExt' => 'jpg,jpeg,png',
               'fileMine' => 'image/jpeg,image/png'
           ]])->check(['imgFile' => $file]);
           $info = \think\facade\Filesystem::disk('public')->putFile((string)time(), $file,
               function () use ($file) {
                   $fileName = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
                   return $fileName;
               });
           return json(array('code' => 1, 'msg' => '上传成功', 'path' => $request->domain() . '/storage/' . $info));
       } catch (\Exception $e) {
           return $this->exceptionHandle($e, '图片上传失败!' . $e->getMessage(), 'json', '');
       }
   }

你可能感兴趣的:(layui+php文件上传)