php&;cURL是否开始在不等待全部读取的情况下向浏览器提供数据?
嗨,我正在尝试通过web服务器上的代理脚本在浏览器中下载文件。Web服务器上的代码是:php&;cURL是否开始在不等待全部读取的情况下向浏览器提供数据?,php,curl,download,downloading-website-files,Php,Curl,Download,Downloading Website Files,嗨,我正在尝试通过web服务器上的代理脚本在浏览器中下载文件。Web服务器上的代码是: <?php $file = 'http://example.com/somefile.mp3'; download($file,2000); /* Set Headers Get total size of file Then loop through the total size incrementing a chunck size */ function download($file,$chunk
<?php
$file = 'http://example.com/somefile.mp3';
download($file,2000);
/*
Set Headers
Get total size of file
Then loop through the total size incrementing a chunck size
*/
function download($file,$chunks){
set_time_limit(0);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-disposition: attachment; filename='.basename($file));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
$size = get_size($file);
header('Content-Length: '.$size);
$i = 0;
while($i<=$size){
//Output the chunk
get_chunk($file,(($i==0)?$i:$i+1),((($i+$chunks)>$size)?$size:$i+$chunks));
$i = ($i+$chunks);
}
}
//Callback function for CURLOPT_WRITEFUNCTION, This is what prints the chunk
function chunk($ch, $str) {
print($str);
return strlen($str);
}
//Function to get a range of bytes from the remote file
function get_chunk($file,$start,$end){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_RANGE, $start.'-'.$end);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'chunk');
$result = curl_exec($ch);
curl_close($ch);
}
//Get total size of file
function get_size($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
return intval($size);
}
?>
当我尝试下载一个大文件(例如超过500mb)时,它等待的时间太长(有些IMTES抛出内存限制500错误),几分钟后,我的浏览器询问保存位置并开始下载
当cURL获得文件的第一块时,我应该如何立即开始在浏览器中下载???我也发布了一个问题,但asnswer没有帮助。它仍然会先将所有内容加载到内存中,然后再发送给浏览器
亲爱的社区,我在这两天里都很累。请帮忙尝试将readfile($file)
放在下载函数的末尾readfile()
首先加载到memeory,然后让我们下载,这样就没有必要使用它了!