Skip to content

Instantly share code, notes, and snippets.

@bueckl
Last active August 29, 2015 14:20
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 bueckl/5fe6e23be7ba48285a48 to your computer and use it in GitHub Desktop.
Save bueckl/5fe6e23be7ba48285a48 to your computer and use it in GitHub Desktop.
Download Files in Folder as ZIP #SS3 #SilverStripe
/**
* @return SS_HTTPRequest
*/
public function assetbackup() {
$name = $this->Title . '.zip';
$tmpName = TEMP_FOLDER . '/' . $name;
$zip = new ZipArchive();
if(!$zip->open($tmpName, ZipArchive::OVERWRITE)) {
user_error('Client Export Extension: Unable to read/write temporary zip archive', E_USER_ERROR);
return;
}
$files = new IteratorIterator(
new RecursiveDirectoryIterator(
$_SERVER['DOCUMENT_ROOT'] . '/assets/' . $this->Event()->ID,
RecursiveDirectoryIterator::SKIP_DOTS
)
);
foreach($files as $file) {
$local = str_replace(ASSETS_PATH . '/', '', $file);
if ( $file->getfileName() != "_resampled") {
$zip->addFile($file, $local);
}
}
if(!$zip->status == ZipArchive::ER_OK) {
user_error('Client Export Extension: ZipArchive returned an error other than OK', E_USER_ERROR);
return;
}
$zip->close();
ob_flush(); // fix browser crash(?)
$content = file_get_contents($tmpName);
unlink($tmpName);
return SS_HTTPRequest::send_file($content, $name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment