Php下载zip存档大文件大小
你好 我有一个带有多个复选框的表单,它允许用户在提示下载zip文件之前选择他们想要的小册子。zip存档下载功能工作正常。但我意识到下载会话将在不下载整个文件(比如8MB)的情况下终止,下载的zip文件将被破坏 参考以下代码,这并不能解决问题。任何人都可以建议我需要查看哪些设置,或者我是否遗漏了任何功能Php下载zip存档大文件大小,php,Php,你好 我有一个带有多个复选框的表单,它允许用户在提示下载zip文件之前选择他们想要的小册子。zip存档下载功能工作正常。但我意识到下载会话将在不下载整个文件(比如8MB)的情况下终止,下载的zip文件将被破坏 参考以下代码,这并不能解决问题。任何人都可以建议我需要查看哪些设置,或者我是否遗漏了任何功能 if($send) { // Make sure program execution doesn't time out // Set maximum script exe
if($send) {
// Make sure program execution doesn't time out
// Set maximum script execution time in seconds (0 means no limit)
set_time_limit(0);
$post = $_POST;
$file_folder = "pdf/"; // folder to load files
$zip = new ZipArchive(); // Load zip library
$zip_name = "File-".time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){ // Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time<br/>";
}
foreach($post['brochure'] as $file){
$zip->addFile($file_folder.$file); // Adding files into zip
}
$zip->close();
if(file_exists($zip_name)){
// set headers push to download the zip
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header('Content-type: application/zip');
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
header("Content-Length: ".filesize($zip_name));
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
$fp = @fopen($zip_name, "rb");
if ($fp) {
while(!feof($fp)) {
print(fread($fp, 1024*8));
flush(); // this is essential for large downloads
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
}
如果($send){
//确保程序执行没有超时
//以秒为单位设置最大脚本执行时间(0表示没有限制)
设置时间限制(0);
$post=$\u post;
$file_folder=“pdf/”;//要加载文件的文件夹
$zip=new ZipArchive();//加载zip库
$zip_name=“File-”.time().“.zip”//zip名称
如果($zip->open($zip_name,ZIPARCHIVE::CREATE)!==TRUE){//打开zip文件以加载文件
$error.=“*抱歉,此时创建ZIP失败
”;
}
foreach($post['宣传册]]作为$file){
$zip->addFile($file_folder.$file);//将文件添加到zip
}
$zip->close();
如果(文件存在($zip\u名称)){
//设置头推送下载zip文件
标题(“Pragma:public”);
标题(“到期日:0”);
标头(“缓存控制:必须重新验证,后检查=0,前检查=0”);
标头(“缓存控制:公共”);
标题(“内容类型:应用程序/zip”);
标题(“内容传输编码:二进制”);
标题('Content-Disposition:attachment;filename=“.”.$zip_name.'”);
标题(“内容长度:”.filesize($zip_name));
readfile($zip_名称);
//临时路径中存在删除zip文件
取消链接($zip_name);
$fp=@fopen($zip_name,“rb”);
如果($fp){
而(!feof($fp)){
打印(fread($fp,1024*8));
flush();//这对于大型下载非常重要
如果(连接状态()!=0){
@fclose($文件);
模具();
}
}
@fclose($文件);
}
}
您是否尝试过这种解决方案,而不是制作“readfile()”
if(file_exists($zip_name)){
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header('Content-type: application/zip');
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
header("Content-Length: ".filesize($zip_name));
echo file_get_contents($file);
exit
}
对我来说,它更简单,而且在很多时候对我来说效果很好
在这之后,我看到的问题是您:
unlink($zip_name);
$fp = @fopen($zip_name, "rb");
if ($fp) {
...
}
这部分代码永远不应该继续,因为您在打开文件之前取消了文件链接!而不是创建“readfile()”您尝试过这种解决方案吗
if(file_exists($zip_name)){
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header('Content-type: application/zip');
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
header("Content-Length: ".filesize($zip_name));
echo file_get_contents($file);
exit
}
对我来说,它更简单,而且在很多时候对我来说效果很好
在这之后,我看到的问题是您:
unlink($zip_name);
$fp = @fopen($zip_name, "rb");
if ($fp) {
...
}
这部分代码永远不应该继续,因为您在打开文件之前取消了链接!首先,您的
if($fp){…}
块永远不应该执行,因为您刚刚执行了取消链接($zip_name);
,所以这个块是无用的
第二,我建议你更换
readfile($zip_name);
与
这将防止出现意外的边界效果
最后,如果您仍然有损坏的归档文件(我相信),那么我认为这是因为在下载刷新之前、期间或之后发生了一些PHP错误(请注意,您的PHP代码在下载之后似乎没有结束,因此代码可能会继续,并且在下载之后可能会发生一些错误)。如果出现此类PHP错误或PHP通知,则PHP错误消息已通过流式处理输出。这很容易检查:使用文本编辑器打开损坏的存档,以查看二进制内容,然后PHP错误消息应在存档的二进制内容的最开始或最底部清晰可读。无需在HEXA模式下打开存档。首先,您的
if($fp){…}
块不应执行,因为您刚刚执行了取消链接($zip_name);
。因此此块没有用
第二,我建议你更换
readfile($zip_name);
与
这将防止出现意外的边界效果
最后,如果您仍然有损坏的归档文件(我相信),那么我认为这是因为在下载刷新之前、期间或之后发生了一些PHP错误(请注意,您的PHP代码在下载之后似乎没有结束,因此代码可能会继续,并且在下载之后可能会发生一些错误)。如果出现此类PHP错误或PHP通知,则PHP错误消息已通过流式处理输出。这很容易检查:使用文本编辑器打开损坏的存档,以查看二进制内容,然后PHP错误消息应在存档的二进制内容的最开始或最底部清晰可读。无需在HEXA模式下打开存档。设法解决我的以下代码问题。基本上,我不应该在调用fopen之前取消文件链接
if($send) {
// Make sure program execution doesn't time out
// Set maximum script execution time in seconds (0 means no limit)
set_time_limit(0);
$post = $_POST;
$file_folder = "pdf/"; // folder to load files
$zip = new ZipArchive(); // Load zip library
$zip_name = "File-".time().".zip"; // Zip name
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){ // Opening zip file to load files
$error .= "* Sorry ZIP creation failed at this time<br/>";
}
foreach($post['brochure'] as $file){
$zip->addFile($file_folder.$file); // Adding files into zip
}
$zip->close();
if(file_exists($zip_name)){
// set headers push to download the zip
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header('Content-type: application/zip');
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
header("Content-Length: ".filesize($zip_name));
//readfile($zip_name);
// remove zip file is exists in temp path
//unlink($zip_name);
$fp = @fopen($zip_name, "rb");
if ($fp) {
while(!feof($fp)) {
echo fread($fp, 8192);
flush(); // this is essential for large downloads
if (connection_status()!=0) {
@fclose($zip_name);
die();
}
}
@fclose($zip_name);
}
unlink($zip_name);
}
}
if($send){
//确保程序执行没有超时
//以秒为单位设置最大脚本执行时间(0表示没有限制)
设置时间限制(0);
$post=$\u post;
$file_folder=“pdf/”;//要加载文件的文件夹
$zip=new ZipArchive();//加载zip库
$zip_name=“File-”.time().“.zip”//zip名称
如果($zip->open($zip_name,ZIPARCHIVE::CREATE)!==TRUE){//打开zip文件以加载文件
$error.=“*抱歉,此时创建ZIP失败
”;
}
foreach($post['宣传册]]作为$file){
$zip->addFile($file_folder.$file);//将文件添加到zip
}
$zip->close();
如果(文件存在($zip\u名称)){
//设置头推送下载zip文件
标题(“Pragma:public”);
标题(“到期日:0”);
标头(“缓存控制:必须重新验证,后检查=0,前检查=0”);
标头(“缓存控制:公共”);
标题(“内容类型:应用程序/zip”);
标题(“内容传输编码:Bi