我有这个代码:
if (isset($_GET['file']) && isset($_GET['name']))
{
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
curl_close($ch);
if (preg_match('/Content-Length: (\d+)/', $data, $matches))
{
// Contains file size in bytes
$contentLength = (int)$matches[1];
}
header("Content-type: application/x-file-to-save");
header("Content-Disposition: attachment; filename=".$_REQUEST['name']);
header('Content-Length: ' . $contentLength);
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Connection: Keep-Alive');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0', false);
header('Cache-Control: private', false);
readfile($file);
}
问题是 $file 位于不同的服务器上。 如何启用恢复此下载? 谢谢!
最佳答案
修改你的 curl 以也拉入 body (删除 NOBODY 选项)
然后您可以将返回的内容 ($data) 拆分为标题和正文 - 拆分是第一个空行(查找连续两个回车符 - 上面的内容是标题,下面的内容是 body )。将它们放入变量 $header 和 $body。
在 $header 上运行你的 preg_match。
要输出其余部分,只需“echo $body”或 substr($body, $startpos) 即可恢复下载。
可恢复下载:如果文件很大,那么您可能希望将其缓存在本地。 (即将“$body”保存到本地文件,然后在本地文件上使用 readfile。然后您可以通过打开文件 (fopen) 并转到正确的位置 (fseek) 并读取 (fread) 来处理可恢复下载/从那里回显(回显)其余部分。一旦找到起点,fpassthru 也应该做同样的事情。
此外,如果您试图将其他人的内容冒充为您自己的内容,请先获得许可 ;)(否则为什么不简单地将用户引导至其他 URL 并为您自己节省进出带宽)
https://stackoverflow.com/questions/11337534/