内存不足,用flysystem编写文件压缩文件

时间:2018-04-25 17:24:17

标签: php ziparchive flysystem

我正在编写一个工具,用于将用户上传的图像收集到zip存档中。为此,我遇到了Flysystem的ZipArchiveAdapter,它似乎做得很好。

当zip存档中的文件数量达到数千时,我遇到内存限制问题。

当用户的图像数量开始超过1000时,由于可用内存耗尽,通常会失败。为了达到处理少于1000张图像的大多数用户的程度,我已将内存限制增加到4GB,但增加它超出此范围并不是一个真正的选择。

此时简化代码:

<?php
use League\Flysystem\Filesystem;
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
use League\Flysystem\Memory\MemoryAdapter;

class User {
    // ... Other user code

    public function createZipFile()
    {
        $tmpFile = tempnam('/tmp', "zippedimages_");
        $download = new FileSystem(new ZipArchiveAdapter($tmpFile));

        if ($this->getImageCount()) {
            foreach ($this->getImages() as $image) {
                $path_in_zip = "My Images/{$image->category->title}/{$image->id}_{$image->image->filename}";
                $download->write($path_in_zip, $image->image->getData());
            }
        }
        $download->getAdapter()->getArchive()->close();
        return $tmpFile;

        // Upload zip to s3-storage
    }
}

所以我的问题: a)有没有办法让Flysystem写入zip文件&#34;随时随地&#34;到磁盘?目前,当对象被销毁时,它会在写入磁盘之前将整个zip存储在内存中。

b)我应该利用另一个更好的库吗?

c)我应该采取另一种方法吗?例如,让用户下载多个较小的拉链而不是一个大拉链。 (理想情况下,我希望他们只下载一个文件)

0 个答案:

没有答案
相关问题