【问题标题】:PHP on the fly ZIP downloads as blankPHP on the fly ZIP 下载为空白
【发布时间】:2011-12-12 11:58:18
【问题描述】:

似乎我成功地创建了一个即时 zip 存档(filesize 和 file_exists 都返回预期值)但是当我尝试实际下载它时,我收到一个空的 ZIP 文件。 奇怪的是,readfile 和 fread 都会出现这个错误。这是我的代码

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

这适用于

【问题讨论】:

  • php错误日志有什么要说的?
  • 试试 fpassthru。还有下载需要多长时间?

标签: php download zip on-the-fly


【解决方案1】:

为避免占用过多内存,您可以使用ZipStreamPHPZip,它们会将压缩文件按块发送到浏览器,而不是在 PHP 中加载整个内容然后发送压缩文件文件。

这两个库都是不错且有用的代码。一些细节:

  • ZipStream“工作”仅适用于内存,但如果需要,不能轻松移植到 PHP 4(使用 hash_file()
  • PHPZip 将临时文件写入磁盘(占用的磁盘空间与要添加到 zip 中的最大文件一样多),但如有必要,可以轻松适应 PHP 4。

相关的 SO 问题:

【讨论】:

    【解决方案2】:

    问题是 PHP 限制。检查内存和执行时间限制。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-10
    • 2023-03-06
    • 2011-10-22
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 2016-02-06
    • 1970-01-01
    相关资源
    最近更新 更多