phplaravelimageconcatenationlaravel-5.5

How to make unique name for image during upload in laravel?


I am using laravel 5.5 and uploading image. My code is generating name in wrong way.

 $image_icon = $request->file('image_icon');
 $data['image'] = $image_icon->getClientOriginalName().'.'.time();
 $destinationPath = public_path('/images');
 $image_icon->move($destinationPath, $data['image']);

Output name of image is like : heart.png.1544074437

Name should be : heart1544074437.png


Solution

  • try this one by using pathinfo function

    extract file name ..

    $fileName = pathinfo($image_icon->getClientOriginalName(), PATHINFO_FILENAME);
    

    extract extenstion

    $extension = pathinfo($image_icon->getClientOriginalName(), PATHINFO_EXTENSION);
    

    create new file name.

    $fullFileName = $fileName."-".time().$image_icon->getClientOriginalExtension();
    

    for more information see this question