1

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.

1
  • You shouldn't have /public/ in your URLs. public should be the server's DocumentRoot for your project. Commented Aug 10, 2018 at 10:04

1 Answer 1

1

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());
Sign up to request clarification or add additional context in comments.

Comments

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.