Reputation: 301
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
Upvotes: 2
Views: 7483
Reputation: 399
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);
Upvotes: 0
Reputation: 868
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;
Upvotes: 0
Reputation: 19
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
Upvotes: 0
Reputation: 1255
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.
Upvotes: 0
Reputation: 156
try this
$imgName = md5(str_random(30).time().'_'.$request->file('image_icon')).'.'.$request->file('image_icon')->getClientOriginalExtension();
Upvotes: 0
Reputation: 9853
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;
Upvotes: 0
Reputation: 1981
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
Upvotes: 0
Reputation: 15115
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
Upvotes: 2