Zip like files into chunks

Say you have a folder of images and you want to create zip files that contain X number of files.

Just add to the glob brace list of file extensions.

<?php

// The number of files to chunk (+1) e.g. 19 will result in zips of 20 files.
$num_files = 19;
$a_files = glob("*.{jpg,jpeg,JPG,JPEG,gif,GIF,png,PNG,tif,TIF,tiff,TIFF}", GLOB_BRACE);

function zipFiles($zipfiles) {
    //print("IF cnt=$cnt\n");
    $zipfiles = rtrim($zipfiles, " ");
    $curdate = date("Y-m-d-H-i-s-") . preg_replace("/ */", "", microtime());
    print("zip i-$curdate.zip \"$zipfiles\"\n\n");
    exec("zip i-$curdate.zip \"$zipfiles\"");
    exec("rm -f \"$zipfiles\"");
    $GLOBALS['zipfiles'] = "";
    $GLOBALS['cnt'] = 0;
}

$cnt = 0;
$zipfiles = "";
foreach ($a_files as $k=>$pic) {
    if ( $cnt > $num_files ) {
        zipFiles($zipfiles);
    } else {
        //print("ELSE cnt=$cnt\n");
        $zipfiles .= "\"{$pic}\" ";
        $cnt++;
    }
}

if ( $zipfiles != "" ) {
    zipFiles($zipfiles);
}

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.