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

ramher101's avatar

call to undefined Symfony\Component\HttpFoundation\BinaryFileResponse::header()

I got this specific error Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header()

when I followed this article https://laravel.io/forum/04-23-2015-securing-filesimages

I was trying to access images in laravel storage and using their path as an img src.

Following the guide, I wrote this in one controller

    public function getFile($filename)
    {
      return response()->download(storage_path($filename), null, [], null);
    }

routes

    Route::get('file/{filename}', 'CustomersController@getFile')->where('filename', '^(.+)\/([^\/]+)$');

it doesnt return any image so I tried debugging by accessing http://localhost/hulugan.DEV/public/file/img/placeholders-thumbnail/h.jpg

then it leads to the call to undefined method error

What should I do to fix the error

0 likes
7 replies
bobbybouwmann's avatar

It looks like it's not passing the correct path to the download call. Can you dd() the $filename and post that here? It might miss a slash somewhere or the path might be incorrect.

ramher101's avatar

First I got rid of the 'file' in the route so its just like

Route::get('{filename}', 'CustomersController@getFile')->where('filename', '^(.+)\/([^\/]+)$');

I DD'ed what you told me and got this whenever I access the route {filename} "C:\xampp\htdocs\hulugan.DEV\storage\img/placeholders-thumbnail/h.jpg"

Without DD whenever I access http://localhost/hulugan.DEV/public/img/placeholders-thumbnail/h.jpg

I get

(1/1) FatalThrowableError Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header()

Then I tried messing with the route something like http://localhost/hulugan.DEV/public/file/img/placeholders-thumbnail/h.jpg (notice I inserted file)

Then I got this

(1/1) FileNotFoundException The file "C:\xampp\htdocs\hulugan.DEV\storage\file/img/placeholders-thumbnail/h.jpg" does not exist

bobbybouwmann's avatar

Well the problem is your filesystem. Right now you have Windows and Linux working against each other. If you look closely add the dd path you can see that the slashes are in two directions

C:\xampp\htdocs\hulugan.DEV\storage\img/placeholders-thumbnail/h.jpg

This is the problem here! You need to fix that. Not really sure how to do that on Windows, I would recommend to use Homestead or Laragon so you work in a Linux environment or one that is like your production server.

anilcancakir's avatar

Yes @bobbybouwmann. It's true. You are using windows. You can fix this for windows but you need to change coming path string. And you can use php constants which is DIRECTORY_SEPARATOR for detecting the true separator for your file system.

My idea create a global helper function for fixing this problem in everywhere. Let's look my example

function path_fixer($path) {
    // Laravel uses / separator by default.
    
    if (DIRECTORY_SEPARATOR != '/') { // Let's check the current system default is this.
        return str_replace('/', DIRECTORY_SEPARATOR, $path); // Change the separator for current system.
    }

    return $path; // Use coming path.
}
ramher101's avatar

@bobbybouwmann I cannot change the working environment to Linux because its not the default environment from where Im doing my internship, but I will suggest it to them if I will not be able to solve this.

@anilcancakir I followed your function, placed it in helpers, called it in the get file function and it looks like this

 public function getFile($filename)
    {
      $test = Hulugan::path_fixer(storage_path($filename));
      return response()->download($test, null, [], null);
    }

by adding DD it outputs "C:\xampp\htdocs\hulugan.DEV\storage\img\placeholders-thumbnail\h.jpg"

but whenever I remove the DD and access the address I still receive the same message

(1/1) FatalThrowableError Call to undefined method Symfony\Component\HttpFoundation\BinaryFileResponse::header()

Please or to participate in this conversation.