草庐IT

PHP zip 下载错误

coder 2023-12-24 原文

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

这是我的脚本。

    $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>将其呈现为无效的 zip 文件。

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

我用Fiddler调试了一下,请求都是一样的,输出却不一样,为什么?

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

浏览器:

  • 海豚(+测试版)
  • 火狐
  • 默认安卓浏览器
  • 船浏览器

测试的 PHP 版本:

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

这让我抓狂。


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

阿利克斯写道: *> 如果您在提供文件之前注释掉退出调用会怎样?

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

Alix 写道:

If this doesn't work, you may wanna change exit('<h1>Nothing to export</h1>'); to exit(var_dump($_REQUEST));, that way you may check for possible bugs in the form submission by inspecting the Zip file.

很有趣。它只打印 cookie 和 $_GET 参数。如果我将代码放在脚本开头的建议中以及将文件添加到 zip 的 block 中,它会打印所有 $_POST变量。

这里显然存在 PHP 部分的错误。它应该在 exit 时终止被称为但它没有。请注意,这只发生在 Opera 以外的移动浏览器上。我现在要哭了。

最佳答案

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

另外,你应该先ob_[end_]clean()然后输出标题,而不是相反:

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

此外,您可能需要设置这些 header :

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

如果这不起作用,您可能需要更改 exit('<h1>Nothing to export</h1>');exit(var_dump($_REQUEST)); ,这样您就可以通过检查 Zip 文件来检查表单提交中可能存在的错误。

关于PHP zip 下载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12735532/

有关PHP zip 下载错误的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby - 如何使用 Ruby aws/s3 Gem 生成安全 URL 以从 s3 下载文件 - 2

    我正在编写一个小脚本来定位aws存储桶中的特定文件,并创建一个临时验证的url以发送给同事。(理想情况下,这将创建类似于在控制台上右键单击存储桶中的文件并复制链接地址的结果)。我研究过回形针,它似乎不符合这个标准,但我可能只是不知道它的全部功能。我尝试了以下方法:defauthenticated_url(file_name,bucket)AWS::S3::S3Object.url_for(file_name,bucket,:secure=>true,:expires=>20*60)end产生这种类型的结果:...-1.amazonaws.com/file_path/file.zip.A

  3. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  4. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  5. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  6. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

  7. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  8. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  9. ruby - 安装 Ruby 时遇到问题(无法下载资源 "readline--patch") - 2

    当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub

  10. ruby-on-rails - Rails 5 Active Record 记录无效错误 - 2

    我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa

随机推荐