Reputation: 43
I am trying to display images from the storage/app/public folder. Whenever my controller retrieves the images form this folder with
$images = Storage::files('public');
The files retrieved from here are prefixed with public/
, rather than storage/
.
The problem this causes is that an image can be found localhost/storage/filename.jpg
, but cannot be found with localhost/public/filename.jpg
or localhost/storage/public/filename.jpg
.
I can resolve this issue by manually cutting off the public prefix and replacing it with storage, but this isn't a particularly graceful way of doing things.
I have run the command php artisan storage:link and rerunning it results in a The "public/storage" directory already exists.
message.
Upvotes: 1
Views: 1004
Reputation: 8415
Laravel comes with a built-in public
disk in the storage configuration.
To list all files in the storage/app/public
folder without prefixes, use:
\Storage::disk('public')->files();
To get the correct URL of files inside the storage/app/public
folder, use:
\Storage::disk('public')->url($file);
To return the array of URLs of files inside the storage/app/public
folder, here is an example combination:
array_map(function ($f) {
return \Storage::disk('public')->url($f);
}, \Storage::disk('public')->files());
Upvotes: 1