Create a zip file in the Magento export folder

If you haven’t done so already, it’s a good idea to create a separate, password protected directory outside of the Magento structure where you can keep misc scripts that are not part of the Magento hierarchy.

Here’s a snippet that will create a zip archive of a file and remove the original path before it creates the zip.


// ----------------------------------------------------------
// Zip the file
//-----------------------------------------------------------

$filename = "PATH/TO/ORIGINAL.TXT";

$zip = new ZipArchive();
$zipfile = "../var/export/ARCHIVENAME.zip";  // the Magento export folder

if ($zip->open($zipfile, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$zipfile>\n");
}

if(file_exists($zipfile)) {unlink($zipfile); } // removes old zip first
$localname = substr($filename,strrpos($filename,'/') + 1); // removes path from file

$zip->addFile($filename, $localname); // first parameter is path to the actual file, second is the path & filename that the file will have in the archive

echo "<br>$filename archived as $localname in $zipfile - status: " . $zip->status . "\n";

$zip->close();

Leave a Reply