Skip to content

Instantly share code, notes, and snippets.

@N-Porsh
Created December 11, 2013 12:26
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 N-Porsh/7909530 to your computer and use it in GitHub Desktop.
Save N-Porsh/7909530 to your computer and use it in GitHub Desktop.
PHP: files to ZIP
<?php
if(isset($_GET['id']) && $_GET['id'] != ''){
$usr_dir = "upload/".$_GET['id']."/";
$error = "";
//checking if zip module loaded
if(extension_loaded('zip')){
$zip = new ZipArchive(); // подгружаем библиотеку zip
$zip_name = time().".zip"; // имя файла
if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){
$error .= "* Sorry ZIP creation failed at this time";
}
//adding files to archive. from usr_dir
$dirHandle = opendir($usr_dir);
while (false !== ($file = readdir($dirHandle))) {
if ($file != "." && $file != "..") {
$zip->addFile($usr_dir.$file);
}
}
$zip->close();
if(file_exists($zip_name)){
// give user a window to download file
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// deleting zip file if it exists
unlink($zip_name);
}
}
else
$error .= "* You dont have ZIP extension";
}
else echo "Choose correct user";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment