【发布时间】:2012-02-11 04:29:48
【问题描述】:
是否可以在 PHP 中强制下载远程文件而不将其读入内存?我知道 fpassthru()、readfile()、file_get_contents() 都是先将文件读入内存,然后再输出到浏览器中。
这是我的代码:
if($url = getRemoteFileURL($file_id))
{
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="abc.zip"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Pragma: no-cache');
readfile($url); // is there a better function ?
}
我不想做 header("Location: ") 因为那会暴露 URL
【问题讨论】:
-
您可以使用远程文件的分块获取,并分发碎片。但这需要您的脚本发出多个 http 请求,而且效率很低。
标签: php http-headers buffer remote-access readfile