Slash
Slash

Reputation: 49

Uploaded images not loading Laravel 7

I have a function in my laravel site where i submit form data to a database and save the submitted image in the storage directory. The data is stored correctly in the database, but somehow the images wont load and i dont know what to do anymore since this has been bugging me for a couple of days now.

If someone can see and tell me what i did wrong, that would be great!

I've created a symlink using php artisan link:storage

filesystems.php

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'storage/public',
        'visibility' => 'public',
    ],

PostController.php with the store function:

public function store(Request $request)
{

    request()->validate([
        'title' => ['required', 'string', 'max:255',],
        'thumbnail' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048|unique:posts',
        'iframe' => ['required', 'string', 'max:255',],
    ]);

    $image = $request->file('thumbnail');
    $originalname = $image->getClientOriginalName();

    Storage::disk('public')->put($originalname, file_get_contents($image));


    Post::create([
        'title' => $request['title'],
        'thumbnail' => $originalname,
        'iframe' => $request['iframe'],
    ]);

    return redirect()->route('adminPost.index');
}

This is how i am trying to load the image:

<img width="100px" src="{{asset('storage/public/'.$post->thumbnail)}}">

This is where my image is saved This is the path in the browser

Upvotes: 2

Views: 753

Answers (3)

Slash
Slash

Reputation: 49

I found out what the problem was... Somehow my symlink didn't work correctly. The uploads were only in my storage/app/public and not in my public/storage folder, i deleted the public/storage folder and made a new symlink.

Deleting the directory:

rm public/storage

Making a new symlink:

php artisan storage:link

Upvotes: 2

Ankit Jindal
Ankit Jindal

Reputation: 4030

I think the problem is the <image> tag in html.

<img src="{{ Storage::url("/storage/app/{$post->thumbnail}") }}" alt="{{ $post->filename }}" />

Refer to the laravel documentation for more details on Retrieving Files from storage

Upvotes: 0

Ayoub Moum
Ayoub Moum

Reputation: 1

Did you add the enctype attribute on your form element ?

if not add the following ===> enctype="multipart/form-data"

Upvotes: 0

Related Questions