I want to download remote file to browser using cURL, the file is over 400 MB. In the code below cURL waits to download (too long) all file to memory and only then browser asks where to locate it and starts download it.
How to fix the code so as cURL receives first chunk, it tells to the browser to start download immediately without waiting it download all to php's memory so?
The code:
<?php
header("Content-Disposition: attachment; filename=\"How to Use Git and GitHub Videos.zip\"");
header("Content-type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
$url = "http://zips.udacity-data.com/ud775/How%20to%20Use%20Git%20and%20GitHub%20Videos.zip";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $buffer) {
echo $buffer;
return strlen($buffer);
});
curl_exec ($ch);
curl_close($ch);
?>
You can eliminate the problem (and not even use curl): Write a redirect to the actual file, instead of downloading it yourself and then sending it. You'll save bandwidth, memory and time.