图像上传重复

时间:2017-09-27 14:06:06

标签: php laravel laravel-5 laravel-5.4

我正在尝试允许用户在审核产品时上传三张图片(可选)。我遇到了下面的代码问题,即提交表单时图像重复,这意味着我有时会得到两个相同的图像或三个相同的图像。有谁知道这个问题的解决方案或更好的方法来进行三个图像可选图像上传?

Html:

{{Form::label('image', 'Image(optional')}}
{{Form::file('image')}}
{{Form::label('image2', 'Image(optional')}}
{{Form::file('image2')}}
{{Form::label('image3', 'Image(optional')}}
{{Form::file('image3')}}

Laravel PHP:

 $picture = new Picture();
        if($request->hasFile('image')){
            $image = $request->file('image');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $location = public_path('images/' . $filename);
            Image::make($image)->save($location);
            $picture->image = $filename;
        }
        $picture->products()->associate($product);
        $picture->user_id = $request->user()->id;
        $picture->reviews()->associate($review);
    $picture->save();

    $picture2 = new Picture();
    if($request->hasFile('image2')){
        $image = $request->file('image2');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $location = public_path('images/' . $filename);
        Image::make($image)->save($location);
        $picture2->image = $filename;
    }
    $picture2->products()->associate($product);
    $picture2->user_id = $request->user()->id;
    $picture2->reviews()->associate($review;

    $picture2->save();
    $picture3 = new Picture();
    if($request->hasFile('image3')){
        $image = $request->file('image3');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $location = public_path('images/' . $filename);
        Image::make($image)->save($location);
        $picture3->image = $filename;
    }
    $picture3->products()->associate($product);
    $picture3->user_id = $request->user()->id;
    $picture3->reviews()->associate($review);

    $picture3->save();

0 个答案:

没有答案