告诉浏览器在cURL接收到第一个文件块时开始下载


Tell to browser to start download as cURL receives first chunk of file?

我想使用cURL下载远程文件到浏览器,文件超过400 MB。在下面的代码中,cURL等待下载(太长)所有文件到内存,然后浏览器询问在哪里找到它并开始下载。

如何修复代码,以便cURL接收第一个块,它告诉浏览器立即开始下载,而不是等待它下载所有到php的内存?

代码:
<?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);
?>

你可以消除这个问题(甚至不使用curl):写一个重定向到实际文件,而不是自己下载然后发送。你将节省带宽、内存和时间。

<?php
$url = "http://zips.udacity-data.com/ud775/How%20to%20Use%20Git%20and%20GitHub%20Videos.zip";
header("Location: $url");
?>