Created
December 11, 2013 12:26
-
-
Save N-Porsh/7909530 to your computer and use it in GitHub Desktop.
PHP: files to ZIP
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
<?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