PHP Create Zip file


We will zip yyyymmdd_myfilename.txt as yyyymmdd_myfilename.zip below.
$exportfname = date("Ymd")."_myfilename.txt";
$exportzname = str_replace(".txt", ".zip", $exportfname);
 
if (file_exists($exportzname))
    unlink($exportzname);
 
$zip = new ZipArchive();
$filename = "./$exportzname";
if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE)
    die("cannot open $filename\n");
$zip->addFile("./$exportfname","/$exportfname");
$z_n = $zip->numFiles;
$z_s = $zip->status;
$zip->close();
 
echo "* numfiles: " . $z_n .  "\n";
echo "* status:" . $z_s .  "\n";
code snippets are licensed under Creative Commons CC-By-SA 3.0 (unless otherwise specified)

Web master on 2009-06-27 14:49:33
there is also such function:
http://www.phpinform.com/2009/06/27/php-classfunction-to-dynamically-create-a-zip-file-archive/

crhis on 2010-08-20 15:00:20
dude thank you so much i went through tuto's for two days now and your example was so true it's unbeleivable, you rock man:)