PHP zip下载错误

问题描述:

我有一个脚本,可以让用户根据请求下载一个zip文件。它在计算机浏览器上100%运行,但不适用于Android /移动浏览器,仅限Opera(移动)。PHP zip下载错误

这是我的脚本。

$leads = new Packer($zip_name); 
$index = 1; 
$count = 0; 
foreach($cLeads->lastUnitInfo['leads'] as $lead) 
{ 
    // build a request string 
    $export = 'export_lead_'.$index; 
    $req = $_POST[$export]; 

    // add it to the zip file 
    if(isset($req) && $req == '1') 
    { 
     // debug only 
     //echo 'adding lead: '.$lead['file_name'].'<br />'; 
     $leads->addLead('leads/'.$lead['file_name'],$lead['item_name']); 
     $count++; 
     //echo 'count: '.$count.'<br/>'; 
    } 
    $index++; 
} 

// debug 
//exit('count: '.$count); // displays same results on all browsers. 

// we got anything packed ? 
if($count <= 0) //// <-------- BLOCK OF BUG ON MOBILE PHONE 
{ 
    if(file_exists($zip_name)) 
     unlink($zip_name); // delete the zip file created. 
    exit('<h1>Nothing to export</h1>'); 
} ///// <---------------------- END BLOCK 

// download the leads here. 
$leads->purge(); 
exit; 

这是我的purge()功能

public function purge($zip_name = 'leads.zip') 
    { 
    header('Content-type: application/zip'); 
    header('Content-Disposition: attachment; filename="'.$zip_name.'"'); 
    ob_clean(); 
    flush(); 
    readfile($this->zip_name); 

    // errors will be disabled here 
    unlink($this->zip_name); 
    } 

在我的Android手机,zip文件被下载,但包含<h1>Nothing to export</h1>这使得它作为一个压缩文件无效。

所以我的问题是,它是如何只执行在移动浏览器(除Opera),然后继续下载的时候它应该有exited,如果$count为零?

我使用Fiddler调试它,请求都一样,但输出不同,为什么?

这是PHP中的错误吗?因为如果你看我的代码,函数purge()应该输出错误,说明头文件已经发送,但它只是继续下载zip文件。

浏览器:

  • 海豚(+测试版)
  • 火狐
  • 默认Android浏览器
  • 舟浏览器

PHP版本的测试:

  • 5.3.13(生产,共享服务器)
  • 5.1.4

现在这是我发疯。


@Alix这是我见过的最奇怪的错误。我不认真看到这里有任何逻辑错误。对于脚本启动下载,实际上必须将文件添加到zip。现在在我的手机上,它表示没有文件被添加,但temp文件夹中有一个zip文件。此外,如果没有添加文件($count = 0),那么脚本应该终止(因此exit()函数),只有一条消息<h1>Nothing to export</h1>。但它继续下载压缩文件(目前不存在,但在它的临时文件夹中)。 zip文件最终被损坏,因为它包含<h1>Nothing to export</h1>

克斯写道: * >如果您注释掉服务文件之前退出通话会发生什么?

它说readfile错误,然后以乱码的UNICODE字符清除zip文件。我可以告诉它是zip文件,因为它以PK开头,并包含正在导出的图像的名称。

克斯写道:

如果这不工作,你可能想改变exit('<h1>Nothing to export</h1>');退出(的var_dump($ _ REQUEST));,这样你可以检查在表单提交可能的错误通过检查Zip文件。

有趣。它只打印cookie和$ _GET参数。如果我将脚本中的代码放入脚本的开始位置,并在将文件添加到zip的块中,则会打印所有$_POST变量。

这里显然有一个PHP的错误。它应该在调用exit时终止,但它不会。并且介意你,这只发生在Opera以外的移动浏览器上。我现在要流血了。

+0

你是否发送'no-cache'缓存头?一些浏览器已知会导致文件不会被缓存,因此在将其从用户临时目录中删除之后将其下载。 – Brombomb

+0

我补充说,希望消除这个问题。但是,唉,它似乎没有工作。 – TheRealChx101

+0

我似乎有同样的问题。下载在桌面上运行良好,但Android或iOS浏览器似乎与现在的数据下载超时。 – Codeguy007

您确定其他浏览器正在将$_POST[$export]设置为正确的值吗?

此外,你应该先ob_[end_]clean()然后输出头,而不是周围的其他方法:如果不

header('Content-Length: ' . intval(filesize($zip_name))); 
header('Content-Transfer-Encoding: binary'); 

public function purge($zip_name = 'leads.zip') 
    { 
    ob_end_clean(); 
    flush(); 
    header('Content-Type: application/zip'); 
    header('Content-Disposition: attachment; filename="'.$zip_name.'"'); 
    readfile($this->zip_name); 

    // errors will be disabled here 
    unlink($this->zip_name); 
    } 

此外,您可能想设置这些报头工作,你可能想要将exit('<h1>Nothing to export</h1>');更改为exit(var_dump($_REQUEST));,这样你可以通过检查Zip文件来检查表单提交中可能存在的错误。

+0

'$ _POST [$ export]'已设置。我用这段代码在所有浏览器中检查它。 'exit('count:'。$ count)'导致'count:2'。但让我感到困惑的是进入表达式计算为'false'的块,然后调用exit(),但不终止,而是继续下载zip文件。 – TheRealChx101

+0

@ chx101:我一直在盯着代码,我无法理解为什么它可能会在某些浏览器上失败。我非常怀疑这是一个PHP错误tho(算术逻辑明智会很明显)。也许还有一些其他代码(或订单)没有透露?另外,如果您在提供文件之前注销'exit'调用会发生什么情况? –