Creating KMZ File on the Fly (PHP)

My KML file was getting large and consuming time/bandwidth to download. So needed a quick solution and found KMZ. KMZ is a compressed KML file which is treated like KML by Google Earth.  Here is a php code to create KMZ file from KML on the fly. My KML of 6.0 Mb was reduced to 600Kb.(Happy Face)

<?php

header('Content-Type: application/vnd.google-earth.kmz');
header('Content-Disposition: attachment; filename="test.kmz"');

$kmlString="This is your KML string";

$file = "test.kmz";
$zip = new ZipArchive();

if ($zip->open($file, ZIPARCHIVE::CREATE)!==TRUE) {
exit("cannot open <$file>\n");
}
$zip->addFromString("doc.kml", $kmlString);
$zip->close();
echo file_get_contents($file);

?>

Leave a comment