Irakli Qiria
Irakli Qiria

Reputation: 11

How can I upload images in public folder?

I trying upload images with move(public_path()) it saved absolute path example:

C:\Users\user\Desktop\test\web\public\images/product\64b14097befca.png

and when I try display this image with asset() it prints this url http://127.0.0.1:8000/C:\Users\user\Desktop\test\web\public\images/product\64b14097befca.png

if ($request->hasFile('images')) {
    foreach ($uploaded_images as $upload_image) {

        $image_hash_name = uniqid().'.'.$upload_image->getClientOriginalExtension();
        $path = $upload_image->move(public_path('images/product'), $image_hash_name);
            
        $image = new Image;
        $image->image = $path;
        $image->product_id = $new_product_id->id;
        $image->save();
    }
        return redirect()->route('admin.products.index');
}

Upvotes: 0

Views: 31

Answers (1)

Abdulla Nilam
Abdulla Nilam

Reputation: 38652

In the upload code store only the relative path to the image in the public directory

$image->image = 'images/product/'.$image_hash_name;

and use assert like this

<img src="{{ asset($image->image) }}" alt="Product Image">

Upvotes: 0

Related Questions