2012-07-25 36 views
1

文件下载工作 - 除了大文件下载 - 大文件下载过快,打开时损坏 - 我得到错误信息(for压缩文件):无法打开文件:它看起来不是有效的压缩文件。该文件是高清上传OK并且该文件夹php头文件下载 - 无法打开文件:它似乎不是一个有效的档案

在这是PHP代码我使用强制头下载

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; 
+0

您是否收到错误消息?例如。 abuot最大文件大小或时间? – PeeHaa 2012-07-25 09:30:34

+0

no - 不能看到任何错误 – Jeff 2012-07-25 09:33:39

+0

您是否启用了错误报告? – PeeHaa 2012-07-25 09:34:14

回答

0

首先尝试直接访问文件(网址指向zip文件) 。

如果它也出现错误或损坏,请使用流式写入器一次写入大量数据。

(假设服务器端的文件没有损坏)

或使用这样的事

http://php.net/manual/en/httpresponse.send.php

+0

直接访问文件时正确下载 – Jeff 2012-07-25 09:41:00

0

找到了解决办法

这是一个最大内存分配的问题 - 它有一个PHP默认设置为236M

我加了这个和它的工作

感谢所有帮助

问候

杰夫

+0

只提高内存分配限制是个坏主意。这意味着当有人试图下载文件时,整个文件将被读入内存,这可以在服务器上创建相当的性能。看看这个线程,你可以如何以块的形式读取文件,而不是只将它的一小部分保存在内存中。 http://stackoverflow.com/questions/11647329/php-download-jpg-or-avi/11647520#11647520 – 2012-07-25 10:20:33

0

即使是加大了内存分配的下载量仍然相当不可靠的。我通过使用下面来管理它以保持稳定。欣赏每个人的意见。

header("Content-Disposition: attachment; filename=" . urlencode($file)); 
header("Content-Type: application/force-download"); 
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 
header("Content-Description: File Transfer");    
header("Content-Length: " . filesize($file)); 
flush(); // this doesn't really matter. 

$fp = fopen($file, "r"); 
while (!feof($fp)) 
{ 
echo fread($fp, 65536); 
flush(); // this is essential for large downloads 
} 
fclose($fp);