PHP force download of remote file without reading into memory
Is it possible to force a download of remote file in PHP without reading it into memory? I know fpassthru(), readfile(), file_get_contents() all reads the files into memory before outputting it into the browser.
Here's my code:
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 ?
}
I don't want to do header("Location: ") because that would reveal the URL
If you do a header("Location: ...");
to a downloaded file, the URL isn't actually revealed much, if at all.
Anyway, readfile
is probably your best option. I would assume, given that it writes straight to the output, that PHP proceeds by reading in a part of the file, then outputting it, then reading the next part, etc., overall using very little memory.
上一篇: 用PHP读取远程页面的一部分
下一篇: PHP强制下载远程文件而不读入内存