Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storing and retrieving stored file in Laravel 5.4

Tags:

php

laravel-5

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!

like image 390
jacobdo Avatar asked Sep 19 '25 12:09

jacobdo


1 Answers

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.

like image 83
Robert Avatar answered Sep 22 '25 04:09

Robert