/** * 本地文件上传OSS * @param Request $request * @return bool|IlluminateHttpJsonResponse */ public function localLoadFile(Request $request) { if ($request->hasFile('file')) { if ($request->file('file')->isValid()) { // 上传文件 $path = 'contract' . DIRECTORY_SEPARATOR . date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d'); //上传到oss $image_path = upload_image($path, $request->file('file'), 'oss'); if ($image_path) { return $image_path; } else { return false; } } } return Helper::jsonErrorData(ApiErr::UPLOAD_ERR); } /** * curl上传本地文件到OSS * @param $path * @return array|bool|string */ public function localFile($path) { $data = [ 'file' => new CURLFile(realpath($path)) ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, route('api.esign.testLoadFile'));//此处以当前服务器为接收地址 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 10);//设置最长等待时间 curl_setopt($ch, CURLOPT_POST, 1);//post提交 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $data = curl_exec($ch);//执行 if (curl_errno($ch)) { return curl_error($ch); } curl_close($ch);//释放 return $data; }
上传OSS文件方法:
/** * 上传图片到阿里云或者七牛云 * @param $path 要保存的路径 * @param $file 上传的文件 * @param string $drive 要使用的驱动 * @return bool 图片完全路径 */ function upload_image($path, $file, $drive = 'oss') { if (!$path) return false; $disk = Storage::disk($drive); $path = $disk->put($path, $file); switch ($drive) { case 'qiniu': return $disk->getUrl($path); case 'oss': return $disk->url($path); } }
配置文件: