This is a sample script for creating a zip file which includes all directories and files in a specified directory.
$files = $this->directoryToArray("$path/assets",TRUE);
$zip = new ZipArchive();
if($zip->open($zip_path,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) === true) {
foreach($files as $filenames){
if(!is_dir($filenames)){
$filename = substr($filenames,strrpos($filenames,"assets"));
$zip->addFile("$filenames","package/$filename");
}
}
}
$zip->close();
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=package.zip");
readfile($zip_path);
--------------------------------------------------------------------------
function directoryToArray($directory, $recursive) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory. "/" . $file)) {
if($recursive) {
$array_items = array_merge($array_items, $this->directoryToArray($directory. "/" . $file, $recursive));
}
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
} else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}
$files = $this->directoryToArray("$path/assets",TRUE);
$zip = new ZipArchive();
if($zip->open($zip_path,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) === true) {
foreach($files as $filenames){
if(!is_dir($filenames)){
$filename = substr($filenames,strrpos($filenames,"assets"));
$zip->addFile("$filenames","package/$filename");
}
}
}
$zip->close();
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=package.zip");
readfile($zip_path);
--------------------------------------------------------------------------
function directoryToArray($directory, $recursive) {
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory. "/" . $file)) {
if($recursive) {
$array_items = array_merge($array_items, $this->directoryToArray($directory. "/" . $file, $recursive));
}
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
} else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}