如何在laravel 5.6中将对成员函数getClientOriginalExtension()的调用固定为null

时间:2018-07-17 05:00:18

标签: php laravel-5 dropzone.js

在我的laravel应用程序中,我正在以编程方式使用dropzone上传图像。这是我的控制器,用于将图像存储在VehicleController中

public function store(Request $request)
    {


    $photos = $request->file('file');

        if (!is_array($photos)) {
            $photos = [$photos];
        }

        if (!is_dir($this->photos_path)) {
            mkdir($this->photos_path, 0777);
        }

        for ($i = 0; $i < count($photos); $i++) {
            $photo = $photos[$i];
            $name = sha1(date('YmdHis') . str_random(30));
            $save_name = $name . '.' . $photo->getClientOriginalExtension();//this is line 75
            $resize_name = $name . str_random(2) . '.' . $photo->getClientOriginalExtension();

            Image::make($photo)
                ->resize(250, null, function ($constraints) {
                    $constraints->aspectRatio();
                })
                ->save($this->photos_path . '/' . $resize_name);

            $photo->move($this->photos_path, $save_name);

            $upload = new Upload();
            $upload->filename = $save_name;
            $upload->resized_name = $resize_name;
            $upload->original_name = basename($photo->getClientOriginalName());
            $upload->save();
        }
        return Response::json([
            'message' => 'Image saved Successfully'
        ], 200);

但是当我的表单提交按钮单击后,发生以下错误,

1/1) FatalErrorException

Call to a member function getClientOriginalExtension() on null
in VehicleController.php line 75

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

我认为您的循环不正确。

尝试下面的代码。

$photos = $request->file('file');
foreach($photos as $photo){
    $name = sha1(date('YmdHis') . str_random(30));
    $save_name = $name . '.' . $photo->getClientOriginalExtension();
    $resize_name = $name . str_random(2) . '.' . $photo->getClientOriginalExtension();
}