php头下载-无法打开文件:它似乎不是一个有效的存档


php header download - Cannot open file: it does not appear to be a valid archive

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

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

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

如果它也给出错误或损坏,请使用类似流写入程序一次写入大块数据。

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

或者使用类似的东西

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

找到解决方案

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

我提高了这个和它的工作

感谢的帮助

问候

Jeff

即使增加了内存分配,下载仍然非常不可靠。我设法通过使用下面的方法使它稳定下来。感谢大家的投入。

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);