Cobra_Fast
Cobra_Fast

Reputation: 16111

Dynamically long downloads?

so I have seen this at a few places and sites, that file downloads don't seem to specify a Content-Length, so the download continues until the server stops sending data.

Now I was wondering how I could achieve something like that with PHP, when I'm sending dynamically generated files through a PHP-script for download.

I've done some research but couldn't really find anything.

(As a specific example: I would like to let a user download a ZIP-file while it's still being built.)

So, how do I create a dynamically long download without the serverside knowing how long it'll turn out?

Upvotes: 0

Views: 62

Answers (1)

Shubham
Shubham

Reputation: 22349

As said, you could just skip sending Content-Length header. This function can come in handy:

function force_download($file){
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    //header('Content-Length: ' . filesize($file)); <--- Don't send length
    readfile($file);
}

Upvotes: 1

Related Questions