Laravel 图片上传

控制器中代码:

    /**
     * @Name XXX
     * @Description  上传图片接口
     * @Param picture:图片 @ParamTest 图片文件
     * @apiParam   (params) {String} picture 图片文件
     * @Response 通用格式:{"code":响应码,"message":"错误描述","data":{}}
     * data{
     *    path:"图片地址",
     * }
     *
     */

    public function uploadCompanyImg(Request $request){

        $file = $request->file('picture');
        header('Content-type: application/json');
        // 文件是否上传成功
        if ($file->isValid()) {
            // 获取文件相关信息
            $originalName = $file->getClientOriginalName(); //文件原名
            $ext = $file->getClientOriginalExtension();     // 扩展名

            $realPath = $file->getRealPath();   //临时文件的绝对路径

            $type = $file->getClientMimeType();     // image/jpeg
            $size =$file->getSize();
            $this->_result['code']=101;
            if($size > 2*1024*1024){
                $this->_result['message']='文件大小超过2M';
                echo json_encode($this->_result);
                exit();
            }
            $extArr = array('jpg','jpeg','png','gif');
            if(!in_array($ext,$extArr)){
                $this->_result['message']='文件格式不正确';
                echo json_encode($this->_result);
                exit();
            }

            // 拼接文件名称
            $filename = date('YmdHis') . uniqid() . '.' . $ext;
            // 使用我们新建的upload_company_img本地存储空间(目录)
            //这里的upload_company_img是配置文件的名称
            $bool = Storage::disk('upload_company_img')->put($filename, file_get_contents($realPath));

            if($bool){
                $this->_result['code']=200;
                $this->_result['message']='成功';
                $url='https://api.bxbedu.com/static/study/images/company/'.date('Ym',time()).'/'.$filename;
                $path='/static/study/images/company/'.date('Ym',time()).'/'.$filename;
                $this->_result['data']=array('url'=>$url,'path'=>$path);
                echo json_encode($this->_result);
            }else{
                $this->_result['message']='上传失败';
                echo json_encode($this->_result);
            }

        }else{
            $this->_result['message']='上传失败';
            echo json_encode($this->_result);
        }

    }

/config/filesystems.php的'disks' => 中添加

 'upload_company_img' => [

            'driver' => 'local',
            'url' =>  env('APP_URL').'/static',

            // 文件将上传到storage/app/uploads目录
            //'root' => storage_path('static/study/situation'),

            // 文件将上传到static/study/situation目录 如果需要浏览器直接访问 请设置成这个

            'root' => public_path('static/study/images/company/'.date('Ym',time())),
        ],
原文地址:https://www.cnblogs.com/liyante/p/9327514.html