从base64 PHP PART II的数组创建ZIP文件

时间:2015-10-29 09:32:22

标签: php

我有一个问题。

我有base64数组,它将转换为许多PDF文件。

但问题是如何下载它们一次。

所以也许我能做些什么。

并且其中之一就是将它们全部压缩。

那么如何在PHP中编写base64的ZIP数组。

这是我的代码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);


?>

1 个答案:

答案 0 :(得分:1)

您可以使用此代码:

/* 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