在 PHP 中压缩文件而不超出内存

问题描述 投票:0回答:1

我想做的是迭代链接列表并将它们全部压缩到同一目录中。这些文件包括图像、PDF、音频和视频。问题是某些文件很大,我收到以下错误:

    FatalErrorException: Error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 46512453 bytes)...

这是我现在拥有的:

    $zip = new ZipArchive();
    $tmp_file = tempnam('.','');
    $zip->open($tmp_file, ZipArchive::CREATE);

    foreach($mediaFiles as $file){
        $downloadFile = file_get_contents($file);
        $zip->addFromString(basename($file), $downloadFile);
    }

    $zip->close();
    header('Content-disposition: attachment; filename=download.zip');
    header('Content-type: application/zip');
    readfile($tmp_file);
php memory zip
1个回答
0
投票

检查这个库,它允许创建 zip 文件并将它们作为流返回:

https://www.phpclasses.org/package/6110-PHP-Create-archives-of-compressed-files-in-ZIP-format.html

© www.soinside.com 2019 - 2024. All rights reserved.