You missed the most important part - the output of the file! :)
Add
readfile('test.zip');
to the end of the php file.
Also, the HTTP content length header is not computed correctly:
header("Content-Length: ".filesize($zip));
This will always give you 0 (or false), since filesize expects the file name as an argument.
Change the line to:
header("Content-Length: ".filesize('test.zip'));
After that, both files will load successfully and contain two files. For completeness, here is a complete working example:
$string1 = 'Some data some data some data';
$string2 = 'Some data some data some data';
$zip = new ZipArchive();
$filename = "test.zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFromString("string1.txt", $string1);
$zip->addFromString("string2.txt", $string1);
$zip->close();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
clearstatcache();
header("Content-Length: ".filesize('test.zip'));
readfile('test.zip');
source
share