mercredi 21 juin 2017

How to avoid same name photo replacement on Laravel

I have a static method fromForm() basically dealing with the uploaded pic on my form. Pics are being stored on public/images/photos

Here is the method fromForm()

/**
* Create a photo object and move the pics from the upload form.
*
* @return photo object
*/
public static function fromForm(UploadedFile $file){

    $photo = new static;

    $name = $file->getClientOriginalName();

    $photo->path=$photo->baseDir.'/'.$name;

    $file->move($photo->baseDir, $name);

    return $photo;
}

Then processing to db on my controller with an addPhoto() method

/**
* Image validation, processing to database.
*
* @return redirect 
*/
    public function addPhoto($geo, $street, Request $request){

        $this->validate($request, [

            'photo' => 'required|image'
        ]);

        $photo = Photo::fromForm($request->file('photo'));

        Flyer::locatedAt($geo, $street)->addPhoto($photo);

        return redirect('/{$geo}/images');

    }

My question is: How can I avoid photos with the same name from being replaced on my folder?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2rDDvDx
via IFTTT

Aucun commentaire:

Enregistrer un commentaire