1

I am trying to display image stored in my local. Those file uploaded are in Storage folder

Storage folder

I am using the follow code to display the image

@foreach($publications as $p)
    <div class="card" style="width: 18rem;">
        <img class="card-img-top" src="{{URL('public/storage/app/{$p->file_name}')}}" alt="{{$p->file_name}}" />
      <div class="card-body">
        <p class="card-text">{{$p->description}}</p>
      </div>
    </div>
@endforeach

But I got:

enter image description here

what's wrong?

EDIT 1

I use php artisan storage:link, the console displayed

The [public/storage] directory has been linked.

But it still not working

3
  • Maybe you forgot link your storage/app/public folder to public folder. Try run php artisan storage:link Commented Jun 23, 2019 at 17:18
  • @LeventeOtta not working but the console displayed The [public/storage] directory has been linked. Commented Jun 23, 2019 at 17:21
  • How do your public folder looks like? (Add a screenshot if possible) Commented Jun 23, 2019 at 18:32

2 Answers 2

4

First you need to do

php artisan storage:link

so that your storage folder which the wepapp cannot access usually links with your public folder so that your app can access the pictures like if they are in the public folder but it will store them in the storage folder then you should put smthg like this to display the image

i think it will be like this

    <img src="/storage/2/{{$p->file_name}}"/>

you could edit the path to the path in your application if this path didnt work

hope it works with you

Edit i will share with you an example from my code in migration the users migration

$table->string('image');

controller

$imageName = time().'.'.request()->image->getClientOriginalExtension();
        request()->image->move(public_path('userpicture'), $imageName);
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'image' => $imageName,
            'password' => Hash::make($data['password']),
        ]);

note that the create method i am using here works only when you put those fildes fillable in the user model so never mind you could use whatever you want in the storing data part but concetrate on this three lines

$imageName = time().'.'.request()->image->getClientOriginalExtension();
            request()->image->move(public_path('userpicture'), $imageName);

                'image' => $imageName,

as you can see the move function is moving the picture to the public folder and putting it in userpicture folder and then the last line store the image name in the database so that i could access it later on to display the picture

and then i could just simply do this to display the picture

<img class="img-responsive img-rounded" src="{{ asset('userpicture/1561335133.jpg') }}" alt="User picture">
Sign up to request clarification or add additional context in comments.

3 Comments

I am not storing the file inside public folfer, as you can see in the first image. Should I store in that folder?
bro the wepapp in laravel doesnot have the prevligies to access folders like storage and thats why we usually link the storage to public because if you want the picture to be accessable you should either put it directly in public or put it in storage then link it to public because public is the folder that the wepapp may access and display its files in your code you missed this point and always remember that css files .js files and pictures should be in public or in storage with link to the public v
i shared my code with you that i wrote two days earlier and it is working fine
2

php artisan storage:link creates a symlink to /storage/app/public

But it looks like your files are in /storage/app/files try moving the files to the public folder.

you can use the asset helper to show the file {{ asset("storage/{$p->file_name}") }}

2 Comments

Not working <img class="card-img-top" src="{{ asset("storage/{$p->file_name}") }}" alt="{{$p->file_name}}" />
if you use {{ asset("storage/2/filename.txt") }}, the file should be located in /storage/app/public/2/filename.txt

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.