skip to Main Content

With any valid binary file (pdf, image, etc) through any route, my download is always 0 bytes. When I store the file or access it otherwise, the file is valid.

If I run a response->download($somePath) on any valid text file (csv, json, txt), The response is valid and correct!

I’ve tried streamDownload(), file(), I’ve even tried manually sending the response without Symfony. I’ve tried using ob_clean(), flush(), etc. I’ve tried several different headers, I’ve tried changing the Cache-Control… nothing works.

This is on a php-fpm, with nginx, php 7.4.

$path = Storage::disk('test')->url('whatever.pdf');
dump(strlen(File::get($path)); // 5115
if (File::exists($path)) {
    $r = response()
        ->download(
            $path,
            'whatever.pdf',
            [
                'content-length' => strlen(File::get($path)),
                'Content-Type' => 'application/pdf'
            ]
        );
    // $r->headers->set('cache-control', 'private');

    return $r; // 0 bytes returned!

}
``

2

Answers


  1. Chosen as BEST ANSWER

    The request was being routed through a node micro server with the response being handled as text.

    Handling the response as binary solved the issue. Previously, this server was only setup to send text.

    res.buffer()
    

  2. Have you tried the following:

    $path = Storage::disk('test')->url('whatever.pdf');
    dump(strlen(File::get($path)); // 5115
    if (File::exists($path)) {
        return response()->download(Storage::disk('test')->path('whatever.pdf'));
    }
    

    The response()->download() method needs an absolute path to the file which I believe the ->url() wasn’t providing.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search