Hello,
The code below creates zip archive and downloads but doesn’t take “sub-folders” into zip archive. Can we fix that? Could you help please?
./folder1/folder2/sub-folder1/sub-folder2/sub-folder3
[php]
$directory = ‘folder1/folder2’; //“DIRECTORY”
$filename = ‘myfilesname’; //“FILENAME” (OPTIONAL)
$d = scandir($directory); //GETS FILES OF DIRECTORY
$z = new ZipArchive(); //ARCHIVES ZIPPING
$zn = $directory.time().’.zip’; //ZIP FILENAME
//STARTS THE ZIPPING PROCESS
if($z->open($zn, ZIPARCHIVE::CREATE)===true){
//DOWNLOADS A SELECTED FILENAME OF THE DIRECTORY
if($filename){
if(file_exists($directory.'/'.$filename)){
$z->addFile($directory.'/'.$filename, $filename);
} else {
echo 'File does not exist.';
}
} else {
//GETS EACH FILE OF THE DIRECTORY
foreach($d as $f){
//REMOVES THE DOTS
if($f!='.'&&$f!='..'){
$z->addFile($directory.'/'.$f, $f);
}
}
}
//CLOSES THE ARCHIVE
$z->close();
//DOWNLOADS THE DIRECTORY AS A ZIP
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zn.'"');
readfile($zn);
unlink($zn);
} else {
echo ‘Folder does not exist.’;
}
[/php]
Best Regads