php,文件下载 [英] php, file download
问题描述
我正在使用简单的文件下载脚本:
I am using the simple file downloading script:
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。
It is working on my localserver upto 200mb.
当我在我的网站上尝试这个代码,它下载173KB而不是200MB的文件。
When i try this code in my website it downloads 173KB instead of 200MB file.
我检查了一切,写了一些自定义代码(使用ob函数和fread而不是readfile),但无法下载大文件。
I checked everything, wrote some custom code (using ob functions and fread instead of readfile) but can't download big files.
感谢您的答案。
- 我正在使用Apache 2.2,PHP 5.3
- 处理大文件的所有PHP设置都可以。 (执行时间,内存限制,...
推荐答案
代码是您无法控制输出流,您让PHP处理它,而不知道背景中正在发生什么:
One issue I have with the following code is you have no control over the output stream, your letting PHP handle it without knowing exactly what is going on within the background:
您应该做什么设置输出系统,您可以控制和复制accros服务器。
What you should do is set up an output system that you can control and replicated accros servers.
例如:
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>";
这只是基本的,但一定要走!
This is only basic but give it a go!
另请阅读我的回覆: file_get_contents => PHP致命错误:允许内存耗尽
这篇关于php,文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!