1

任务:在 s3 服务器上上传图片

代码在我的本地系统上正常运行,文件在 s3 服务器上正确保存,但是当我在服务器上推送此代码时,它给了我 hashName 错误。

Error: Call to a member function hashName() on null in file /var/www/html/doctring-api/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php on line 240

图像助手代码

<?php

namespace App\Helpers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class UserHelper
{
    public static function uploadImage($image)
    {
        try {
            if (count(explode("data:image/", $image)) > 1) {

                $fileName = \Carbon\Carbon::now()->timestamp . '_' . uniqid() . '.' . explode('/', explode(':', substr($image, 0, strpos($image, ';')))[1])[1];
                $image = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '',$image));
                Storage::disk('s3')->put($fileName, $image, 'public');

                return $fileName;
            }

            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
}

API 控制器

 public function uploadPrescription(Request $request){
        $validator = Validator::make($request->all(), [
            'patient_id' => 'required',
            'appointment_id' => 'required',
            'prescription' => 'required'
        ]);

        if($validator->fails()){
            return $this->sendError('Validation Error.', $validator->errors());
        }

        $input = $request->all();

        $status_check = Appointment::where('id','=',$input['appointment_id'])->first();
        if($status_check->status == 'OnGoing'){

            //upload prescription(image in base64) to s3 bucket
            if($request->has('prescription'))
            {
                $imageName = UserHelper::uploadImage($request->prescription);

                $input['image_url'] = $imageName;
            }

            $data=[
                'patient_id' => $input['patient_id'],
                'appointment_id' => $input['appointment_id'],
                'prescription'=>  $imageName
            ];

            //Uploading Prescription only one record
//            $profile = Prescriptions::updateOrCreate(Arr::except($data, 'prescription'), $data);

            $profile = Prescriptions::updateOrCreate($data);
            return response()->json(['message' => "Prescription Uploaded", 'profile'=> $profile, 'error'=> 0, 'status'=> 1 ]);
        }else{
            return response()->json(['message' => "Prescription Uploading Failed", 'error'=> 1, 'status'=> 0 ]);
        }

    }
4

1 回答 1

1

在您的错误报告的第 240 行Filesystem/FilesystemAdapter,有这一行:

return $this->putFileAs($path, $file, $file->hashName(), $options);

这意味着当您尝试上传时 $file 为空。在您的问题中,您说在本地环境中您没有这个问题,所以您应该检查之前的代码,是什么导致生产服务器环境出现问题。

希望这可以帮助

于 2020-07-09T16:47:46.323 回答