Santanu Biswas
Santanu Biswas

Reputation: 181

How to acces files from Storage/App directory in Laravel

I want to access the file storage/app/15573877669649.jpg.

Code to upload the file

$file = $request->file('cover_image');
$fileName = time().rand(111, 9999).".".$file->getClientOriginalExtension();
Storage::disk('local')->put($fileName, file_get_contents($file));

Code to retrieve the file in the blade.php file

<img src = "{{ URL::to('/').Storage::url($article->coverImage) }}" alt="image">

which returns

http://127.0.0.1:8000/storage/15573877669649.jpg

I have tried to manually put http://127.0.0.1:8000/storage/app/15573877669649.jpg but it's also not working

Upvotes: 2

Views: 3425

Answers (2)

Mech Tsai
Mech Tsai

Reputation: 190

By default, Laravel's Storage::('local') is not accessible from public; the Storage::('public') is the one will be publish by command php artisan storage:link

So for your sample code, you can do it like this:

Uploading file

$file = $request->file('cover_image');
$fileName = time().rand(111, 9999).".".$file->getClientOriginalExtension();
Storage::disk('public')->put($fileName, file_get_contents($file));

In blade.php file

<img src = "{{ URL::to('/').Storage::disk('public')->url($article->coverImage) }}" alt="image">

// Or simpler
<img src = "{{ asset("/storage/$article->coverImage") }}" alt="image">

More detail of the 'disk' please check config file in /config/filesystems.php, and refer the the Public Disk section of official document


And, remember to enable the follow symlinks settings in your web server

// Apache:
<Directory>
    Options FollowSymLinks
</Directory>

// Nginx:
{
    disable_symlinks off;
}

Upvotes: 2

JTinkers
JTinkers

Reputation: 1789

  • Open commandline inside your project
  • Type in php artisan storage:link

Your files will now be accessible under "/storage/file.jpg"

/ leads to public

/storage/ leads you to storage/app/public thanks to link

Upvotes: 4

Related Questions