Zip and Download a folder
Posted: Wed Nov 21, 2012 9:00 am
Hi,
I am working on a script to create a .zip archive from a folder and download it. So far, I have the following, which creates and downloads a .zip archive of a pre-defined number of files in an array.
[syntax=php]$files = array('file1.txt', 'file2.txt', 'file3.txt');
$archive = 'archive.zip';
$zip = new ZipArchive;
$zip->open($archive, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$archive);
header('Content-Length: '.filesize($archive));
readfile($archive);
unlink($archive);[/syntax]
How should I modify it so that it will zip and download a folder that contains files and subfolders?
Thanks.
I am working on a script to create a .zip archive from a folder and download it. So far, I have the following, which creates and downloads a .zip archive of a pre-defined number of files in an array.
[syntax=php]$files = array('file1.txt', 'file2.txt', 'file3.txt');
$archive = 'archive.zip';
$zip = new ZipArchive;
$zip->open($archive, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$archive);
header('Content-Length: '.filesize($archive));
readfile($archive);
unlink($archive);[/syntax]
How should I modify it so that it will zip and download a folder that contains files and subfolders?
Thanks.