jacobdo
jacobdo

Reputation: 1615

storing and retrieving stored file in Laravel 5.4

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!

Upvotes: 4

Views: 20376

Answers (1)

Robert
Robert

Reputation: 5963

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.

Upvotes: 14

Related Questions