我想使用 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);
?>