Cuidado con un posible problema en el ejemplo de Adnan: Si el myarchive.zip de destino está en la carpeta de la fuente, entonces usted necesita para excluirlo en el bucle, o para ejecuta el iterador antes de crear el archivo de almacenamiento (si no existe ya). Aquí hay una secuencia de comandos revisada que utiliza la última opción y agrega algunas variables de configuración arriba. Este no debe usarse para agregar a un archivo existente.
<?php
// Config Vars
$sourcefolder = "./" ; // Default: "./"
$zipfilename = "myarchive.zip"; // Default: "myarchive.zip"
$timeout = 5000 ; // Default: 5000
// instantate an iterator (before creating the zip archive, just
// in case the zip file is created inside the source folder)
// and traverse the directory to get the file list.
$dirlist = new RecursiveDirectoryIterator($sourcefolder);
$filelist = new RecursiveIteratorIterator($dirlist);
// set script timeout value
ini_set('max_execution_time', $timeout);
// instantate object
$zip = new ZipArchive();
// create and open the archive
if ($zip->open("$zipfilename", ZipArchive::CREATE) !== TRUE) {
die ("Could not open archive");
}
// add each file in the file list to the archive
foreach ($filelist as $key=>$value) {
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}
// close the archive
$zip->close();
echo "Archive ". $zipfilename . " created successfully.";
// And provide download link ?>
<a href="http:<?php echo $zipfilename;?>" target="_blank">
Download <?php echo $zipfilename?></a>
código genial ... ¿cómo puedo editar esto para ZIP una carpeta anidada excluyendo todas las carpetas que conducen a ella? mi carpeta deseada vive en "/var/www/vhosts/mysite.com/dev/wp-content/themes/mytheme/". cuando ejecuto este script, obtengo una carpeta de inicio de var luego www luego vhosts luego mysite.com etc. ¿Qué me estoy perdiendo? –
Entiendo bien que el código itera también a través de "." y "..." directorios dentro de "temas /"? Parece que esto causa problemas cuando intenta descomprimir el archivo. – Tamara
este código causa un problema al descomprimir el archivo. –