2

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

kapildevsharma
  • 199
  • 1
  • 4
  • 17

8 Answers8

2

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

Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
0

Use pathinfo()

pathinfo — Returns information about a file path

path The path to be parsed.

options If present, specifies a specific element to be returned; one of PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION or PATHINFO_FILENAME.

If options is not specified, returns all available elements.

$image_icon = $request->file('image_icon')->getClientOriginalName();

$filename = pathinfo($image_icon, PATHINFO_FILENAME);
$extension = pathinfo($image_icon, PATHINFO_EXTENSION);
$data['image'] = $filename.time().'.'.$extension;
Sohel0415
  • 9,523
  • 21
  • 30
0

You can to do this:

$image = explode(".", $image_icon);
$image_name = $image[0];
$image_extension = array_slice($image , -1, 1);

$data['image'] = $image_name.time().'.'.$image_extension[0];

I hope this will be helpful and easy solution to your problem. Thanks

Inzamam Idrees
  • 1,955
  • 14
  • 28
0

try this

 $imgName = md5(str_random(30).time().'_'.$request->file('image_icon')).'.'.$request->file('image_icon')->getClientOriginalExtension();
0

Above code need minor improvement as follows :

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

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

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

$data['image'] = $image_icon->getClientOriginalName().time().'.'.$extension;

I have not tested this code snippet, but it should work.

Prabhu Nandan Kumar
  • 1,205
  • 12
  • 22
0

You can also use laravel out of the box solution for Upload:

$request->photo->store('images');

For more check here: https://laravel.com/docs/5.5/requests#files

0

The below code is working fine for me.

// extract file name ..

        $fileName = pathinfo($fileupload_dt->getClientOriginalName(), PATHINFO_FILENAME);
        
        // extract extenstion

        $extension = pathinfo($fileupload_dt->getClientOriginalName(), PATHINFO_EXTENSION);
        
        // create new file name.

        $imageName = $fileName."-".time().".".$fileupload_dt->getClientOriginalExtension();

        $uploadPath = 'public/ArchiveImg/img';
        $fileupload_dt->move($uploadPath,$imageName);
        $imageUrl = $uploadPath.$imageName;
Nazmul Haque
  • 720
  • 8
  • 13
0

the best way is unique name also in the file name

$file= $request->file('image');
$filename= date('YmdHi').$file->getClientOriginalName();
$file->move(public_path('storage/products'), $filename);