A trivial scenario:
1) Symlink public/storage to storage/app/public using
php artisan storage:link
2) Upload file using
$path = $request->file('avatar')->store('avatars');
3) In my understanding, I should be able to access the file under this url - `www.example.com/storage/avatars/avatar.jpg
However, the path I get in the third step is actually public/avatars/avatar.jpg, so I end up replacing the 'public/' part with '' to have a correct path.
It seems that I am missing something, but I just can't find exactly what.
Thank you for your help!
When using the public disk, you should store the file in the public disk.
You can specify the disk in the 2nd parameter of the store function.
$path = $request->file('avatar')->store(
'avatars', 'public'
);
This makes sure that the file is stored in storage/app/public
, the storage
symlink in the public directory points to storage/app/public
.
Now you can simply use:
asset('storage/avatars/avatar.jpg');
And it will retrieve the file from storage/app/public/avatars/avatar.jpg
You might want to check storeAs
to rename the filename or use a subdirectory to make sure your filenames are unique.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With