【问题标题】:How to create large zip files with low memory_limit?如何创建具有低 memory_limit 的大型 zip 文件?
【发布时间】:2023-03-09 19:58:01
【问题描述】:

我需要从 jpg 文件创建一个大型 zip 存档。 zip 生成在 64MB 时中断 - 无法增加主机的内存限制。

我尝试了很多方法来生成 zip 文件,但都失败了:

  • PHP ZipArchive 类。
  • exec("zip -r file.zip *.jpg")
  • pclzip(可以选择生成临时文件而不是内存)

也许还有另一种方法可以使用内存不足的流/临时文件创建 zip 文件?

感谢您的建议

【问题讨论】:

  • 你读过这篇文章吗?它看起来很有帮助而且相当全面。 stackoverflow.com/questions/4357073/…
  • @NickAllen 谢谢,我试过 fopen 也没有用。 :( 我认为在服务器上对机器本身也有限制,不仅在 php 中。

标签: php zip


【解决方案1】:

您可以在脚本中尝试 set_time_limit ( sec ) 或 ini_set('max_execution_time', sec)。

你可以试试这样的

initialise your zip class

foreach ( files in the directory as $idx => $name) {

    add $name to your zip file;

    // every 10 files zipped, reset the max_execution_time
    if ( $idx > 0 && $idx % 10 == 0 ) {
        set_time_limit ( 30 ); //if it doesnt work try to use ini_set('max_execution_time', 30);
    }
}

每压缩 10 个文件,它就会将 max_execution_time 重置为 30 秒。

我以10个文件为例,你可以根据你的平均文件大小来设置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    相关资源
    最近更新 更多