note 86188 modified in function.ziparchive-addemptydir by thiago
| From: | thiago@php.net | Date: | Mon, 08 Nov 2010 15:11:30 +0000 |
| Subject: | note 86188 modified in function.ziparchive-addemptydir by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-173851@lists.php.net to get a copy of this message | ||
I've brought a little modification to dayjo's code, so it wouldn't re-create the
whole structure of your working drive in the zip file:
<?php
// Function to recursively add a directory,
// sub-directories and files to a zip archive
function addFolderToZip($dir, $zipArchive, $zipdir = ''){
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
//Add the directory
if(!empty($zipdir)) $zipArchive->addEmptyDir($zipdir);
// Loop through all the files
while (($file = readdir($dh)) !== false) {
//If it's a folder, run the function again!
if(!is_file($dir . $file)){
// Skip parent and root directories
if( ($file !== ".") && ($file !== "..")){
addFolderToZip($dir . $file . "/", $zipArchive, $zipdir . $file .
"/");
}
}else{
// Add the files
$zipArchive->addFile($dir . $file, $zipdir . $file);
}
}
}
}
}
?>
If you don't specify the third parameter (zipdir), the directory you're adding will be
added at the root of the zip file.
D.Jann
--was--
I've brought a little modification to dayjo's code, so it wouldn't re-create the
whole structure of your working drive in the zip file:
<?php
// Function to recursively add a directory,
// sub-directories and files to a zip archive
function addFolderToZip($dir, $zipArchive, $zipdir = ''){
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
//Add the directory
if(!empty($dir)) $zipArchive->addEmptyDir($dir);
// Loop through all the files
while (($file = readdir($dh)) !== false) {
//If it's a folder, run the function again!
if(!is_file($dir . $file)){
// Skip parent and root directories
if( ($file !== ".") && ($file !== "..")){
addFolderToZip($dir . $file . "/", $zipArchive, $zipdir . $file .
"/");
}
}else{
// Add the files
$zipArchive->addFile($dir . $file, $zipdir . $file);
}
}
}
}
}
?>
If you don't specify the third parameter (zipdir), the directory you're adding will be
added at the root of the zip file.
D.Jann
http://php.net/manual/en/function.ziparchive-addemptydir.php