简体   繁体   中英

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. I thought this might be because of upload_max_filesize, but that is set to 200m and same for post_max_size.

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.

You can also stream the file to the user as it is being created. See php rendering large zip file - memory limit reached

Are you using the addFile() method?

If so, try replacing that call with 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. Looks like I need some better error checking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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