Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

eLekun's avatar

Downloading Files in Laravel

I have tried various ways to download a file from the server but cannot solve it, I get an error calling the undefined method Symfony \ Component \ HttpFoundation \ BinaryFileResponse :: header () In the documentation it says how to implement but still getting error.

 public function descargarExcel()
{
    $filename ='FormatoExcel.xlsx';
   
    $path = storage_path($filename);

    return response()->download($path, $filename);
}
0 likes
3 replies
rodrigo.pedra's avatar
Level 56

Just tried the snippet above and have it working.

At first I thought on maybe you got the file on the wrong path, as usually user files are stored in./storage/app ($path = storage_path('app/'. $filename);) and not just at ./storage.

But I tried with a file in both paths and it works.

The only thing I can think of is broken dependencies.

To test it out:

  • remove you project's ./vendor folder.
  • Run composer install from a terminal at your project's root folder.
  • Remove any .php file from this folder: ./bootstrap/cache inside your project
  • Try downloading the file again.

Another thing that just crossed my mind is if you could have any middleware that manipulates the response.

If you can please run:

php artisan route:list --path=download

Where you should replace download by whatever the route path that runs this code is. For example, if this code is run on a route with a path /admin/users/{user}/profile/report you should run:

php artisan route:list --path=admin/users/{user}/profile/report

And post the results.

Note that I didn't include the route's first forward-slash.

1 like
eLekun's avatar

Indeed if the function worked, execute php artisan route: list --path = "my route" and I had a middleware that was giving problems, I eliminated it and it downloaded without problems, thanks

1 like

Please or to participate in this conversation.