mercredi 27 septembre 2017

Image Upload duplication

I am trying to allow users to upload three images(optional) when they make a review of the product. I am encountering a problem with the code below, which is that the images duplicate when the form is submitted which means that i sometimes get two of the same images or all three the same. Does anyone know the solution to this issue or a better way to do three image optional image uploads?

Html:








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();



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire