Skip to content

Instantly share code, notes, and snippets.

@xurizaemon
Created February 4, 2011 00:52
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 xurizaemon/810547 to your computer and use it in GitHub Desktop.
Save xurizaemon/810547 to your computer and use it in GitHub Desktop.
zipcart 37592
/**
* Zip callback: PHP zip extension
*/
function _zipcart_phpzip($files) {
$zip = new ZipArchive;
$filename = file_directory_path() .'/'. variable_get('zipcart_cache','zipcart') .'/'. time() .'.zip' ;
$zip_open = $zip->open($filename, ZIPARCHIVE::CREATE) ;
if ($zip_open === TRUE) {
dpm('zip opened');
// would be nice to handle duplicate filenames here
foreach ( $files as $file ) {
dpm('file added');
$zip->addFile($file, basename($file));
}
$zip->close();
dpm($zip, 'zip');
$headers = module_invoke_all('file_download', $filename);
if (in_array(-1, $headers)) {
return drupal_access_denied();
}
dpm('zip files permitted');
if (count($headers)) {
// need to remove any content-disposition headers here
foreach ( $headers as $k => $header ) {
if ( stristr($header, 'Content-Disposition:') === 0 ) {
unset($headers[$k]) ;
}
}
$headers[] = 'Content-Disposition: attachment; filename="'. _zipcart_zip_filename() .'"' ;
// FIXME: it would be nice to wait until the file transfer completes before we clear the
// files out of the session. However, if we do that, the user can't add new files to their
// cart until the zip is downloaded. So we'll do this first.
$_SESSION['zipcart']['files'] = array();
file_transfer($filename, $headers);
dpm($filename, 'file transferred');
}
}
else {
drupal_set_message(t('Unable to create file !filename.', array('!filename' => $filename)), 'error');
drupal_access_denied();
return FALSE ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment