问题下载文件

时间:2009-02-23 17:23:11

标签: php download

我编写了一个脚本来强制从网站下载mp3文件。代码工作得很好,但问题是它无法下载大文件。我用9.21mb的文件尝试了它并正确下载,但每当我尝试使用代码下载25mb的文件时,它只是给我一个找不到服务器页面或网站无法显示页面。所以我现在知道下载大文件有问题。下面是执行文件下载的代码段。

header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);             
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=\"".$dname.".mp3\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($secretfile));
$downloaded=readfile($secretfile);  

显示的错误是:HTTP 500内部服务器错误

非常感谢你们的时间。

4 个答案:

答案 0 :(得分:2)

可能是内存限制,但通常PHP会输出一个错误,表示已达到内存限制。

此外,在所有这些之前,如果启用了输出压缩,则应禁用它:

if(ini_get('zlib.output_compression')) {
    ini_set('zlib.output_compression', 'Off');
}

如果启用了输出压缩,IE有时会搞砸。

答案 1 :(得分:0)

观察PHP配置的内存限制和超时

在php.ini中:

memory_limit = 32M
max_execution_time = 300

请注意,如果您希望执行时间非常长,则还需要更改Web服务器超时。

答案 2 :(得分:0)

  

我只是给了我一个找不到服务器页面或网站无法显示页面

这是Internet Explorer显示的错误吗?你有任何服务器端错误吗?您检查了服务器日志吗?

答案 3 :(得分:0)

试试这个:

// empty output buffer
while (ob_get_level()) {
    ob_end_clean();
}
if (ini_get('output_buffering')) {
    ini_get('output_buffering', false);
}

// function to encode quoted-string tokens
function rfc2822_quoteString($string) {
    return '"'.preg_replace('/[^\x00-\x0C\x0E-\x21\x23-\x5B\x5D-\x7F]/', '\\\$0', $string).'"';
}

// HTTP headers
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.rfc2822_quoteString($dname.'.mp3'));
header('Content-Length: '.filesize($secretfile));

// send file
readfile($secretfile);
exit;
相关问题