Last active
August 29, 2015 14:20
-
-
Save bueckl/5fe6e23be7ba48285a48 to your computer and use it in GitHub Desktop.
Download Files in Folder as ZIP #SS3 #SilverStripe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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