Home > other >  Accessing images stored in different files in laravel
Accessing images stored in different files in laravel

Time:12-07

adminuser

I have an admin and a user portal. Admin uploads the member image which has to be displayed in user portal. Both admin and user portal are present in different files. I tried "php artisan storage:link", the image does get stored under public/storage/image but it does not gets displayed in user portal. Any solutions would be helpful.

$member->image = $request->image;

        if ($request->hasfile('image')) {
            $file = $request->file('image');
            $extenstion = $file->getClientOriginalExtension();
            $filename = time() . '.' . $extenstion;
            $file->move('storage/image', $filename);
            $member->image = $filename;
        } 
      $member->save();

   <img src="/storage/image/{{ $member->image }}" width="150" height="150"/>

How should I specify the path for this in user portal?

CodePudding user response:

You can use the asset helper function to generate the URL to the image. This should work if you have run the php artisan storage:link command to create a symbolic link from the public/storage directory to the storage/app/public directory.

Here is how you can use the asset function to generate the URL to the image:

<img src="{{ asset('storage/image/'.$member->image) }}" width="150" height="150" />

Alternatively, you can also use the Storage facade to generate the URL to the image, like this:

<img src="{{ Storage::url('image/'.$member->image) }}" width="150" height="150" />

These solutions should allow you to display the image in the portal.

CodePudding user response:

first, you need to check your file path is where for directory. you can confirm storage link path after move public.

$ cd /var/www/html/public
$ ls -la
$ storage -> /var/www/html/storage/app/public // example

and then, your load file path is correct ?

for example, if you want to load file under storage/app/public/hoge.png, you can load like <img src="{{ asset("storage/hoge.png") }}" >

try those.

  • Related