1

I'm working on a project which has to retrieve some data from the local storage. I'm saving a personal image to the locale storage, but when I'm trying to retrieve the image I'm getting 404 ERROR.

Error: http://localhost:8000/storage/app/img.png

This is the way I'm trying to get the image:

<img src="{{ asset('storage/app/img.png') }}" />

I red the Laravel documentation, but I don't know what is the problem.

daniel19
  • 67
  • 8

1 Answers1

1

Make sure that you are set the symlink with: php artisan storage:link.

Afterwards you can check: <img src="{{ asset('storage/app/img.png') }}" /> or: <img src="{{ asset('storage/img.png') }}" />

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • Symlink to storage is only needed if we're dealing with public folder inside storage. – Sachin Bahukhandi Mar 26 '22 at 10:46
  • @SachinBahukhandi Thanks for comment. But if you use the asset helper function you expect to have access to the public folder or not? – Maik Lowrey Mar 26 '22 at 10:48
  • Depends on the use case. – Sachin Bahukhandi Mar 26 '22 at 10:50
  • That's my problem. I don't want to add it to the public folder. – daniel19 Mar 26 '22 at 10:50
  • ` Opening an image via a browser is very clearly `public` for me. – Maik Lowrey Mar 26 '22 at 10:52
  • The laravel docs said that if you save something into the LOCAL storage you have to use /storage so something like this I think: localhost:8000/storage/x.png. But this also not works for me. – daniel19 Mar 26 '22 at 10:53
  • @daniel19 ok. My mistake. Then I'm afraid I don't know either. Because in such a case I would work with an auth. – Maik Lowrey Mar 26 '22 at 10:54
  • Is there anyway to create a symbolic link for local storage? – daniel19 Mar 26 '22 at 10:55
  • @daniel19 Create a `private/images` folder in your storage/app/ folder. Create A route (`images/private/{image}`) and a new Controller. Maybe named: ImageHandlerController. Inside you will have a public method where you fetch the request and pass the requested image as parameter. Then you can build the path `private/images/' . $image`. To make it private you have to implement auth. That would be the eaysiest way. – Maik Lowrey Mar 26 '22 at 11:06
  • @daniel19 You need a condition to distinguish who can see the picture and who cannot. And Auth would be the simplest way to do that. If you are only allowed to see the picture, then you can query in your method. auth()->user->email === "daniel.xy@email.com" . You could probably also work with tokens but that would only make it more complex. – Maik Lowrey Mar 26 '22 at 11:10
  • @daniel19 Cool! And thank you for feedback. how did you do it? – Maik Lowrey Mar 26 '22 at 11:41