Skip to content

Instantly share code, notes, and snippets.

@torbentschechne
Created August 10, 2018 14:44
Show Gist options
  • Save torbentschechne/bdcdde5866bbf64a9def25802842016d to your computer and use it in GitHub Desktop.
Save torbentschechne/bdcdde5866bbf64a9def25802842016d to your computer and use it in GitHub Desktop.
Create PHP .zip file
/**
* @param array $contents
* @param bool $overwrite
* @return bool
*/
public function create_zip($contents = array(), $overwrite = false) {
$zip_filename = Shopware()->DocPath() . "files/documents" . "/jett_mce_support_".time().".zip";
if (file_exists($zip_filename) && !$overwrite) { return false; }
if ($contents) {
$zip = new \ZipArchive();
if($zip->open($zip_filename, $overwrite ? \ZIPARCHIVE::OVERWRITE : \ZIPARCHIVE::CREATE) !== true) {
return false;
}
foreach($contents as $k => $content) {
$zip->addFromString($k.'.csv', $content);
}
$zip->close();
return ['status' => file_exists($zip_filename), 'file' => $zip_filename];
} else {
return ['status' => false];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment