This section contains the detail about the Zip Directory/Folder completely into a zip file.
Zip Complete Directory/Folder
In this section, you will learn how to zip a directory completely into a zip file.
Example
In this example, we are compressing a directory's whole content into a zip file. For this, we are incorporating a user define function. In which, we are passing the source of the folder as URL and destination URL where zip file will be created.
Given below the code and its output :
<?php $s='ZippedContent/'; $d='ZippedContent/ZipDirectory.zip'; if(Zip($s,$d)) { echo "Directory zipped succesfully"; }else{ echo "error"; } function Zip($source, $destination) { if (extension_loaded('zip') === true) { if (file_exists($source) === true) { $zip = new ZipArchive(); if ($zip->open($destination, ZIPARCHIVE::CREATE) === true) { $source = realpath($source); if (is_dir($source) === true) { $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); foreach ($files as $file) { $file = realpath($file); if (is_dir($file) === true) { $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); } else if (is_file($file) === true) { $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); } } } else if (is_file($source) === true) { $zip->addFromString(basename($source), file_get_contents($source)); } } return $zip->close(); } } return false; } ?>
[ 0 ] Comments