Home > Enterprise >  Download a excel file stored in the storage in php
Download a excel file stored in the storage in php

Time:12-03

I have an excel file present in the storage directory. I want that file to be downloaded once I click on "download excel" button. How can I do that?

CodePudding user response:

Using the "storage_path()", we get the storage path. And then by specifying the path and name we can download the file. We need to pass the header for content-type as 'application/octet-stream'

       $file= storage_path(). "/excelFile.xlsx";
        $headers = array(
                  'Content-Type: application/octet-stream',
                );
        return response()->download($file, 'excelFile.xlsx', $headers);

CodePudding user response:

You can use the Storage Facade from Laravel. https://laravel.com/docs/8.x/filesystem#downloading-files

When using the disk('local') it will look into the storage/app directory.

 public function download()
    {
        return Storage::disk('local')->download('path-to-excel-file', 'filename.xlsx');
    }
  • Related