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

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝