eylay
eylay

Reputation: 2160

Laravel Create Download Link To Storage

I have a file in /storage/excel/exports/abc.xls and I want to create a download link to it, so I wrote:

<a href="{{asset('storage/excel/exports/'.$file)}}" download>{{$file}}</a>

But when I click on the link, it cannot find the file. then I looked at href in Inspect Element and the link was http://localhost/[my-project]/public/storage/excel/exports/abc.xls

And when I cleaned public/ using Inspect Element and changed href value, the link worked fine and it downloaded the file.

I have different ways to solve this issue but i'm sure none of them is best practice.

Whats the best way to create a link to a file in storage folder in laravel?

Upvotes: 3

Views: 6759

Answers (1)

Wild Beard
Wild Beard

Reputation: 2927

If you're on Laravel 5.5 you can use the Storage class. You can then use Storage::url($file) and it should get the proper URL for your file.

However, as per the docs:

Remember, if you are using the local driver, all files that should be publicly accessible should be placed in the storage/app/public directory. Furthermore, you should create a symbolic link at public/storage which points to the storage/app/public directory.

Upvotes: 1

Related Questions