PHP Creating a Zip

2010-02-07

This is how you create and serve a new zip file dynamically in PHP...

Code: (PHP)

$zipFileName = 'tmp.zip';
$fileToInclude = 'files/file.txt';
$textToInclude = 'hello world';

$objZip = new ZipArchive();
$objZip->open($zipFileName, ZIPARCHIVE::CREATE);
$objZip->addFromString('readme.txt', $textToInclude);
$objZip->addFile($fileToInclude);
$objZip->close();

header('Content-disposition: attachment; filename='.$zipFileName);
header('Content-type: application/zip');
echo file_get_contents($zipFileName);

Hope it helps ya :)