更改服务器后readfile()脚本无法正常工作

时间:2012-07-05 04:16:44

标签: php download readfile

我有一个有效的代码,在这里:

if (isset($_GET['file']) && isset($_GET['name']))
{
$file = base64_decode($_REQUEST['file']);

$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');

@readfile($file);

}

而且我将服务器更改为不同的主机提供程序,并且代码停止运行。当我从chrome下载时,它只是保持加载和加载, 但是当我从像IDM这样的下载管理器下载时,它获得了正确的名称和大小,但是当我按下启动时,它会出现一个错误,说明不支持恢复或某些事情......

同样,当我使用不同的提供者时,代码也有效。

1 个答案:

答案 0 :(得分:1)

可能是在readfile()使用ob_clean(); flush();之前输出的错误,以便在readfile

之前删除任何输出