hasFile('image') && $request->file('image')->isValid()) { $path = $request->get('path') ? $request->get('path') . '/' : date('Ym') . '/'; $kuoname = $request->file('image')->getClientOriginalExtension(); $filePath = $request->file('image')->getRealPath(); Log::debug('文件类型'.$kuoname); try{ // if (in_array($kuoname, ['jpg', 'jpeg', 'gif', 'png'])) { // $width = Image::make($filePath)->width(); // $height = Image::make($filePath)->height(); // //要保存的文件名 时间+扩展名 // $filename = time() . '_' . uniqid() . '.' . $kuoname; // $fileurl = $filename . '?' . $width . '_' . $height; // } else { //要保存的文件名 时间+扩展名 $fileurl = $filename = time() . '_' . uniqid() . '.' . $kuoname; // } $imageUrl = Storage::put($path . $filename, file_get_contents($filePath)); Log::debug('upload_url:' . $imageUrl); if ($imageUrl) { return $this->jsonSuccess(['url' => config('customer.chxq_oss_url') . $path . $fileurl]); } else { return $this->jsonError('图片上传失败,请重试'); } }catch (\Exception $exception){ Log::error('upload-exception:'.$exception->getMessage()); return $this->jsonError('图片上传失败,请重试'); }catch (FatalThrowableError $fatalThrowableError){ Log::error('upload-error:'.$fatalThrowableError->getMessage()); return $this->jsonError('图片上传失败,请重试'); } } else { return $this->jsonError('仅支持图片上传,请重试'); } } public function uploadImages(Request $request) { $files = $request->file('image'); $fileCount = count($files); $urls = []; $path = $request->get('path') ? $request->get('path') . '/' : date('Ym') . '/'; for ($i = 0; $i < $fileCount; $i++) { $kuoname = $files[$i]->getClientOriginalExtension(); $filePath = $files[$i]->getRealPath(); Log::debug('文件类型'.$kuoname); //要保存的文件名 时间+扩展名 if (in_array($kuoname, ['jpg', 'jpeg', 'gif', 'png'])) { $width = Image::make($filePath)->width(); $height = Image::make($filePath)->height(); //要保存的文件名 时间+扩展名 $filename = time() . '_' . uniqid() . '.' . $kuoname; $fileurl = $filename . '?' . $width . '_' . $height; } else { //要保存的文件名 时间+扩展名 $fileurl = $filename = time() . '_' . uniqid() . '.' . $kuoname; } $imageUrl = Storage::put($path . $filename, file_get_contents($filePath)); if ($imageUrl) { array_push($urls, config('customer.chxq_oss_url') . $path . $fileurl); } else { Log::debug('文件上传失败。' . $filename); } } if ($urls) { return $this->jsonSuccess($urls); } else { return $this->jsonError('图片上传失败,请重试'); } } }