kuchar kuchar -4 years ago 128
PHP Question

Upload image with different name but keep file extension

When i save image with my form i change the file name. Everything works nice except file extension. When i change file name the extension is missing and file is saved without it. Please help me to solve my problem i want to store also image extension.

This is my code where i update image name.

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

if ($image) {

$imgPath = 'public/post-image/';
$imgName = uniqid('img_');
$store = $request->file('image')->storeAs(
$imgPath, uniqid('img_')
);

$post->image = $imgName;

}

Answer Source

Try this:

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

if ($image) {

    $imgPath = 'public/post-image/';
    $imgName = uniqid('img_') . '.' . $image->extension();
    $store = $image->storeAs($imgPath, $imgName);
    $post->image = $imgName;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download