2
votes

I have created multiple PDF file streams in memory. Can I create a zip file from these streams, and if so how? I want to give the user a downloaded zip of my files without creating any files on the server.

ZipArchive only appears to take file names when running addFile.

1
sounds like you want secure.php.net/manual/en/ziparchive.addfromstring.phpuser6763587

1 Answers

1
votes

I ultimately found my solution here:

Similar to the solution given there, I used the class from phpmyadmin as follows:

header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename='whatever.zip'");

$zip = new ZipFile;
foreach($pdfs as $idx => $content) {
    $zip->addFile($content, "file$idx.pdf");
}
file_put_contents("php://output", $zip->file());