Skip to content

Instantly share code, notes, and snippets.

@kongondo
Forked from somatonic/creat_zip_download.php
Created September 17, 2013 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kongondo/6599572 to your computer and use it in GitHub Desktop.
Save kongondo/6599572 to your computer and use it in GitHub Desktop.
<?php
/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
if(file_exists($destination) && !$overwrite) { return false; }
if(is_array($files)) {
foreach($files as $name => $file) {
if(!file_exists($file)) unset($files[$name]);
}
}
if(count($files)) {
$zip = new ZipArchive();
if(!$zip->open($destination, $overwrite
? ZIPARCHIVE::OVERWRITE
: ZIPARCHIVE::CREATE))
return false;
foreach($files as $name => $file) {
$zip->addFile($file,$name);
}
$zip->close();
return $destination;
} else {
return false;
}
}
// get a language to export json files
$lang = $languages->get("de");
$files_to_zip = array();
// "language_files" = file field on language page where json's are stored
foreach($lang->language_files as $f){
$files_to_zip[$f->name] = $f->filename;
}
$zip = create_zip($files_to_zip, $config->paths->root . 'language_files_de.zip' ,true);
if($zip) wireSendFile($zip); // send file to browser (/wire/core/functions.php)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment