Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Demers94's avatar

Proper way to store public files

Hi,

I'm trying to store an image uploaded by the user in the public folder so that it can be viewed by anyone, and save the path on the Article model.

Here is the code that I tried using first :

$path = request()->file('file')->store('public/articles');

This places the image in the /storage/app/public/articles folder as I expected, but the problem is that the path it returns is equal to public/articles/ABC123.png. After I created the symbolic link to my public folder, I can't retrieve the file by using the assets() method because the path is incorrect. It tries to find the file in /public/articles, but with the symlink it's in /storage/articles instead.

I now use this instead :

$path = request()->file('file')->store('public/articles');
$path = str_replace('public', '/storage', $path);

If I do the above then I get the correct path, /storage/articles/ABC123.png and I can now use this to display the image.

Is this really the proper way to do things? It seems a bit weird to have to use str_replace() just to get a valid path. Is there a better way to achieve this?

Thanks for the help

0 likes
3 replies
Cronix's avatar

remove public from here $path = request()->file('file')->store('public/articles);`

2 likes
Demers94's avatar

Thank you for the fast reply @Cronix

If I do that, two things happen :

  1. It now stores the file in /storage/app/articles instead of /storage/app/public/articles, so I can't access it via the symlink

  2. The path returned is still incorect, so I can't use it to display the file using the asset() helper method

Cronix's avatar

Ah, sorry. You need to specify the public disk when saving.

$path = request()->file('file')->store('articles', 'public');
// $path = 'articles/filename.ext', stored in /storage/app/public/articles/filename.ext

and then

<img src="{{ Storage::disk('public')->url($model->path) }}">
// yoursite.com/storage/articles/filename.ext

use the storage facade to resolve the url of things saved in storage. The asset helper(s) start from site root. https://laravel.com/docs/5.7/filesystem#file-urls

2 likes

Please or to participate in this conversation.