我使用的是简单的文件下载脚本:
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}它可以在我的本地服务器上运行到200mb。
当我在我的网站上尝试这个代码时,它下载了173KB而不是200MB的文件。
我检查了所有内容,编写了一些自定义代码(使用ob函数和fread而不是readfile),但无法下载大文件。
感谢您的回答。
发布于 2011-04-08 21:36:13
我对以下代码的一个问题是,你无法控制输出流,你让PHP在不确切知道后台发生了什么的情况下处理它:
您应该做的是设置一个输出系统,您可以控制和复制accros服务器。
例如:
if (file_exists($file))
{
if (FALSE!== ($handler = fopen($file, 'r')))
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: chunked'); //changed to chunked
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
//header('Content-Length: ' . filesize($file)); //Remove
//Send the content in chunks
while(false !== ($chunk = fread($handler,4096)))
{
echo $chunk;
}
}
exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";这只是基本的,但试一试吧!
也可以在这里阅读我的回复:file_get_contents => PHP Fatal error: Allowed memory exhausted
发布于 2011-04-08 21:23:16
看起来readfile在处理长文件时可能会有问题。正如@Khez所问的,可能是脚本运行的时间太长了。一次快速的Googling搜索产生了几个分块文件的示例。
http://teddy.fr/blog/how-serve-big-files-through-php http://www.php.net/manual/en/function.readfile.php#99406
发布于 2012-12-01 05:05:37
对于某些情况,一种解决方案是,您可以使用PHP-script智能地决定从何处下载文件,但您可以将重定向返回到客户端,然后客户端包含直接链接,该链接由web服务器单独处理,而不是直接从PHP发送文件。
这至少可以通过两种方式完成:PHP脚本将文件复制到“下载区域”中,例如,其他一些后台/服务脚本可能会定期从“旧”文件中清除该区域,或者您将真正的永久位置暴露给客户端。
当然,每种解决方案都有缺点。根据客户端(curl、wget、GUI浏览器)请求的文件,它们可能不支持您所做的重定向,而在另一种情况下,文件非常暴露于外部世界,并且可以在没有PHP脚本(访问)控制的情况下随时读取。
https://stackoverflow.com/questions/5595485
复制相似问题