3

我有这个代码:

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 位于不同的服务器上。如何启用恢复此下载?谢谢!

4

1 回答 1

0

修改你的卷曲以也拉入身体(删除 NOBODY 选项)

然后,您可以将返回的内容 ($data) 拆分为标题和正文 - 拆分是第一个空行(连续查找两个回车 - 上面的内容是标题,下面的任何内容都是正文)。将它们放入变量 $header 和 $body 中。

在 $header 上运行 preg_match。

要输出其余部分,只需“echo $body”或 substr($body, $startpos) 即可恢复下载。


可恢复下载:如果文件很大,那么您可能希望在本地缓存它。(即保存“$body”到本地文件,然后在本地文件上使用readfile。然后您可以通过打开文件(fopen)并转到正确的位置(fseek)并读取(fread)来处理可恢复下载/从那里回显(回显)其余部分。一旦找到起点,fpassthru 也应该这样做。

此外,如果您试图将其他人的内容作为自己的内容传递,请先获得许可;)(否则为什么不简单地将用户引导到另一个 URL 并为自己节省进出带宽)

于 2012-07-05T04:44:22.990 回答