简体   繁体   English

PHP ZipArchive限制为10mb

[英]PHP ZipArchive limited to 10mb

I have a project where the user can add multiple files to a cart and then zip them into a single file and download all of them at once. 我有一个项目,用户可以在其中将多个文件添加到购物车,然后将它们压缩为一个文件,然后一次下载所有文件。

It seems though that as soon as the cart gets bigger then 10mb it cannot create the zip file. 看来,一旦购物车变得更大然后10mb,它就无法创建zip文件。 I thought this might be because of upload_max_filesize, but that is set to 200m and same for post_max_size. 我以为这可能是由于upload_max_filesize引起的,但是对于post_max_size来说设置为200m。

What would be limiting this? 有什么限制呢?

Here is my code. 这是我的代码。 It creates a random file name, then checks the cart loop and adds each file. 它创建一个随机文件名,然后检查购物车循环并添加每个文件。

    // create object
$zip = new ZipArchive();

$filename = "loggedin/user_files/ABX-DOWNLOAD-".rand(1, 1000000).".zip"; 

while (file_exists($filename)):                   
    $filename = "loggedin/user_files/ABX-DOWNLOAD-".rand(1, 1000000).".zip"; 
endwhile;      


// open archive 
if ($zip->open('../'.$filename, ZIPARCHIVE::CREATE) !== TRUE) {
    die ("Could not open archive");
}


// add files
//echo $all_audio;

    foreach($audio_ids as $audio_id){
        if($audio_id != ""){
            $audio = Audio::find_by_id($audio_id);
            $selected_category        = Categories::find_by_id($audio->category_id);
            $selected_sub_category    = Sub_Categories::find_by_id($audio->sub_category_id);
            $selected_sub_sub_category    = Sub_Sub_Categories::find_by_id($audio->sub_sub_category_id);

            $f = "uploads/mp3/".$selected_category->category_name."/".$selected_sub_category->sub_category_name."/".$selected_sub_sub_category->sub_sub_category_name."/".$audio->media_path; 
             $zip->addFile($f) or die ("ERROR: Could not add file: $f");  

        }
    }

// close and save archive
$zip->close() or die("ERROR: COULD NOT CLOSE");
 }

If the archive is being created in memory, you can increase the PHP memory limit through PHP.ini or other means, or alternatively write the file directly to disk while it is being created. 如果归档文件是在内存中创建的,则可以通过PHP.ini或其他方式增加PHP的内存限制,或者在创建文件时将文件直接写入磁盘。

You can also stream the file to the user as it is being created. 您还可以在创建文件时将文件流式传输给用户。 See php rendering large zip file - memory limit reached 查看php渲染大型zip文件-达到内存限制

Are you using the addFile() method? 您是否正在使用addFile()方法?

If so, try replacing that call with addFromString() + file_get_contents() . 如果是这样,请尝试使用addFromString() + file_get_contents()替换该调用。

Well, narrowed it down. 好吧,缩小范围。 A pathname was corrupted and was making the zip error out because the file did not exist. 路径名已损坏,由于文件不存在,导致出现zip错误。 Looks like I need some better error checking. 看来我需要一些更好的错误检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM