Ryan Arief
Ryan Arief

Reputation: 1045

Create ZIP file from array of base64 PHP PART II

i have a question.

i have array of base64 which will transform to many PDF file.

but the problem is how to download them once at all.

so maybe there is something i can do.

and one of all is to zip them all.

so how to ZIP array of base64 in PHP.

here is my code file.php

<?php
 //$pdf= 'JVBERi0xL...sdfsdgfh';



header('Content-Type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename = file_".$kb.".pdf");

echo base64_decode($pdf);


?>

Upvotes: 2

Views: 1192

Answers (1)

Kiyarash
Kiyarash

Reputation: 2578

You cn use This code :

/* creates a compressed zip file */
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; }
    //vars
    $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,$file);
        }
        //debug
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

        //close the zip -- done!
        $zip->close();

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

Above Code reference

http://php.net/manual/en/zip.examples.php

Upvotes: 1

Related Questions