php header download - 无法打开文件:它似乎不是有效的存档

时间:2012-07-25 09:28:05

标签: php apache file-upload

文件下载正在运行 - 除了更大的文件下载 - 大文件下载太快并且在打开时损坏 - 我收到错误消息(对于zip文件):无法打开文件:它似乎不是有效的存档。该文件是def上传确定并位于文件夹

这是我用来强制下载标题的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;

3 个答案:

答案 0 :(得分:0)

首先尝试直接访问该文件(直接URL到zip文件)。

如果它也出现错误或损坏,请使用类似的东西 流编写器一次写入大量数据。

(假设服务器端文件未损坏)

或者使用这样的东西

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

答案 1 :(得分:0)

找到解决方案

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

我提高了这个及其工作

感谢所有帮助

此致

杰夫

答案 2 :(得分: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); 
相关问题