doutou7549 2011-12-12 11:58
浏览 67
已采纳

PHP即时ZIP下载为空白

It seems I successfully create an on-the-fly zip archive (filesize and file_exists both return the expected values) but when I attempt to actually download it, I receive an empty ZIP file. Curiously enough this error occurs with both readfile and fread. This is my code

$filename = $zip;
    $handle = fopen($filename, 'r');
    if ($handle === false)
    {
        die('Could not read file "' . $filename . '"');
    }

    header('Content-type: application/zip');
    header('Content-Disposition: attachment; filename="fsdownload.zip"');
    header('Cache-Control: private');
    while (!feof($handle))
    {
        echo fread($handle, 8192);
    }
    fclose($handle);

This works fine for zip-Files < 10 MB. Any thoughts on what the problem might be?

  • 写回答

2条回答 默认 最新

  • donglinli2027 2011-12-12 12:57
    关注

    To avoid consuming too much memory, you can use ZipStream or PHPZip, which will send zipped files on the fly to the browser, divided in chunks, instead of loading the entire content in PHP and then sending the zip file.

    Both libraries are nice and useful pieces of code. A few details:

    • ZipStream "works" only with memory, but cannot be easily ported to PHP 4 if necessary (uses hash_file())
    • PHPZip writes temporary files on disk (consumes as much disk space as the biggest file to add in the zip), but can be easily adapted for PHP 4 if necessary.

    Related SO questions:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 MIT控制器能控制不稳定系统吗
  • ¥15 公司代码X对业务伙伴X无效,处理方法?
  • ¥15 微信内链接跳转到浏览器打开怎么实现
  • ¥15 三角波可以直接加施密特电路整形到矩形波吗实物
  • ¥15 html,php,在使用html请求php文件时发生了错误,无法请求到php文件读取数据库并用javascript进行数据显示,刷新
  • ¥15 touchsocket udp组播
  • ¥20 MAC怎么安装Silverlight 插件?以及安装了怎么启用
  • ¥15 VS2012中查询语句无法填入解析,数值传不进去
  • ¥15 gis系统开发出现命名空间“ESRI.ArcGIS”中不存在类型或命名空间名“Analyst3D”报错
  • ¥15 怎么让ai定时给我发信息 c#或者python