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

8 Answers 8

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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;

Comments

0

try this

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

Comments

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.

Comments

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

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.