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

thebigk's avatar
Level 13

response()->download($pathToFile); not working

I want people to be able to download files from CDN. When the user clicks 'Download' button on my app, I initiated an ajax request to the server. On the server I've -

public function download(Resource $resource) {
        // Force download of the file
        $file_to_download   = 'https://data.domain.com/downloads/' . $resource->file_name;
        return response()->download($file_to_download);
    }

I've double checked that the file actually is available on the https://data.domain.com/downloads/filename.ext and is accessible from browser. Yet, Laravel reports the following error -

"Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException"

The URL reported by the error is actually correct, and file does exist on that location. Can someone tell me what am I missing?

Would really appreciate your responses.

0 likes
9 replies
thebigk's avatar
Level 13

Thank you, @Snapey - but none of those options seem to be working. All I could get was the file contents in the response; but I want to force download on user's browser.

Snapey's avatar

Wow, you tried all those quick?

Even the ugly, slow one where the file is downloaded to the local server first should work.

thebigk's avatar
Level 13

Yep; been at it for about 2 hours. Downloading to local server, unfortunately isn't an option. Is there other way?

Else, I'll have to store the files and perform cleanup through cron. But I'd rather opt for a simpler solution.

I tried the html5 'download' attribute as well. It doesn't work. Files open in new browser tab instead of downloading.

thebigk's avatar
Level 13

Update: I'm trying to get this to work -

    public function download(Resource $resource) {
        // Force download of the file
        $this->file_to_download   = 'https://data.domain.com/downloads/' . $resource->file_name;
        return response()->streamDownload(function () {
            echo file_get_contents($this->file_to_download);
        }, 'file-name.extension');
    }

This does get the contents of the file, but doesn't force download on user's end.

I'm following Laravel's example from this -https://laravel.com/docs/5.6/responses#streamed-downloads

thebigk's avatar
Level 13

Thanks. Would 'stream' be a good way to download, if the files are say 50 MB + ? Most of the files will be ZIP and PDFs. I think it's okay to let PDFs open in a new tab; and ZIP files will trigger a dialog anyway.

thebigk's avatar
Level 13

Update:

Finally decided to download the files to local server and then delete after send. I think the following should work; but it doesn't --

public function download(Resource $resource) {
        // Force download of the file
        $file_to_download   = 'https://data.domain.com/downloads/' . $resource->file_name;
        $temp_file_location =   public_path('/tmp_files/' . $resource->file_name);
        copy($file_to_download, $temp_file_location);
        return response()->download($temp_file_location);
    }

The tmp_files is the location under 'public' folder.

Here's the overall flow -

  1. Ajax request is made which triggers the download method on my controller (above)

  2. The response to the above request actually works (can see the file contents in the 'Response' , in Chrome's inspector). But the download never begins.

What's wrong?

thebigk's avatar
Level 13

Can anyone help? Above code should work; but it doesn't. I've no clue why. Would really appreciate some help.

Please or to participate in this conversation.