<?php
$proj_root = $_SERVER['DOCUMENT_ROOT'].'/cms/';
$tmp_folder = $proj_root.'temp/';
$dir_name = $_GET['dir_name'];
if(isset($_GET['sub_dir_name']))
{
$zipname = "Documents-".$_GET['sub_dir_name'].".zip";
}else{
$zipname = "Documents.zip";
}
$files = scandir($dir_name,1);
if(is_array($files)) {
foreach($files as $key=>$file) {
if(file_exists($dir_name."/".$file)) {
if(($file != ".") && ($file != ".."))
{
$valid_files[] = $dir_name."/".$file;
}
}
}
}
if(count($valid_files > 0)){
$zip = new ZipArchive();
$zip_name = $zipname;
if($zip->open($tmp_folder.$zip_name, ZIPARCHIVE::CREATE)!==TRUE){
$error .= "* Sorry ZIP creation failed at this time";
}
foreach($valid_files as $file){
echo basename($file);
$zip->addFile($file,basename($file));
}
$zip->close();
if(file_exists($tmp_folder.$zip_name)){
// force to download the zip
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($tmp_folder.$zip_name);
// remove zip file from temp path
unlink($tmp_folder.$zip_name);
}
} else {
echo "No valid files to zip";
exit;
}
?>