Borrowing from the always brilliant Mr. David Walsh, here is a great little function to create a zip file archive of a given set of files, ready for download from your site.

First, the zip file create function which will be doing all the heavy lifting:

function create_zip($files = array(), $destination = '', $overwrite = false) {
    //if the zip file already exists and overwrite is false, return false
    if (file_exists($destination) && !$overwrite) {
        return false;
    $valid_files = array();
    //if files were passed in...
    if (is_array($files)) {
        //cycle through each file
        foreach ($files as $file) {
            //make sure the file exists
            if (file_exists($file)) {
                $valid_files[] = $file;
    //if we have good files...
    if (count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        //add the files
        foreach ($valid_files as $file) {
            $zip->addFile($file, basename($file));
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
        //close the zip -- done!

        //check to make sure the file exists
        return file_exists($destination);
    } else {
        return false;

Right, now that we have the function, let us set about putting it to use. First, we’ll need an array of fully qualified files to use, for example:

$filestozip = array(

Now we simple run the function, providing it with our array of files to be included, the proposed name of the archive we want to generate, as well as whether or not we should overwrite if it happens to already exist. And if the function returns true, then we can go ahead and link to our newly generated ZIP archive!

$result = create_zip($filestozip,'',true);
if ($result == true){
echo 'Download!';