{"id":128050,"date":"2022-09-12T14:07:41","date_gmt":"2022-09-12T14:07:41","guid":{"rendered":"https:\/\/phppot.com\/?p=19297"},"modified":"2022-09-12T14:07:41","modified_gmt":"2022-09-12T14:07:41","slug":"how-to-create-zip-files-using-php-ziparchive-and-download","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/09\/12\/how-to-create-zip-files-using-php-ziparchive-and-download\/","title":{"rendered":"How to Create Zip Files using PHP ZipArchive and Download"},"content":{"rendered":"<div class=\"modified-on\" readability=\"7.1666666666667\"> by <a href=\"https:\/\/phppot.com\/about\/\">Vincy<\/a>. Last modified on September 12th, 2022.<\/div>\n<p>Creating a zip from a folder full of files can be done in PHP using the ZipArchive class. This class instance creates a handle to read or write files to a compressed archive.<\/p>\n<p>This class includes several properties and methods to zip file archives.<\/p>\n<p>In this article, we will see an example of,<\/p>\n<ol>\n<li>How to create a zip archive file.<\/li>\n<li>How to download the compressed zip file.<\/li>\n<\/ol>\n<p>If you want to know how to <a href=\"https:\/\/phppot.com\/php\/php-compress-image\/\">compress more than one image in PHP<\/a> image compression refer to this earlier article.<\/p>\n<h2>How to create a zip archive file<\/h2>\n<p>This file parses the input directory and compresses its files into a zip file. It proceeds with the following steps to create the zip file of a directory.<\/p>\n<ol>\n<li>Create a PHP ZipArchive class instance.<\/li>\n<li>Open a zip file archive with the instance. It accepts the output zip file name and the mode to open the archive.<\/li>\n<li>Apply a recursive parsing in the input directory.<\/li>\n<li>If the directory includes a file, then it adds to the zip archive using&nbsp;<em>addFile()<\/em>.<\/li>\n<\/ol>\n<p>It handles the use cases of getting the possibilities of being unable to read or archive the directory. Once the zip is created, it displays a message to the browser.<\/p>\n<p class=\"code-heading\">create-zip-file.php<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php\">&lt;?php\n\/\/ Important: You should have read and write permissions to read\n\/\/ the folder and write the zip file\n$zipArchive = new ZipArchive();\n$zipFile = \".\/example-zip-file.zip\";\nif ($zipArchive-&gt;open($zipFile, ZipArchive::CREATE) !== TRUE) { exit(\"Unable to open file.\");\n}\n$folder = 'example-folder\/';\ncreateZip($zipArchive, $folder);\n$zipArchive-&gt;close();\necho 'Zip file created.'; function createZip($zipArchive, $folder)\n{ if (is_dir($folder)) { if ($f = opendir($folder)) { while (($file = readdir($f)) !== false) { if (is_file($folder . $file)) { if ($file != '' &amp;&amp; $file != '.' &amp;&amp; $file != '..') { $zipArchive-&gt;addFile($folder . $file); } } else { if (is_dir($folder . $file)) { if ($file != '' &amp;&amp; $file != '.' &amp;&amp; $file != '..') { $zipArchive-&gt;addEmptyDir($folder . $file); $folder = $folder . $file . '\/'; createZip($zipArchive, $folder); } } } } closedir($f); } else { exit(\"Unable to open directory \" . $folder); } } else { exit($folder . \" is not a directory.\"); }\n}\n?&gt;\n<\/code><\/pre>\n<h3>Output<\/h3>\n<pre><code>\/\/If succeeded it returns Zip file created. \/\/If failed it returns Unable to open directory example-folder.\n[or] \"example-folder is not a director.\n<\/code><\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-19334\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/php-create-zip-550x377.jpg\" alt=\"php create zip\" width=\"550\" height=\"377\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/php-create-zip-550x377.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/php-create-zip-300x206.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/php-create-zip-768x527.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/09\/php-create-zip.jpg 1000w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/p>\n<h2>How to download the compressed zip file<\/h2>\n<p>In the last step, the zip file is created using the PHP ZipArchive class. That zip file can be downloaded by using the PHP code below.<\/p>\n<p>It follows the below steps to download the zip file created.<\/p>\n<ol>\n<li>Get the absolute path of the zip file.<\/li>\n<li>Set the header parameters like,\n<ul>\n<li>Content length.<\/li>\n<li>Content type.<\/li>\n<li>Content encoding, and more.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p class=\"code-heading\">download-zip-file.php<\/p>\n<pre class=\"prettyprint\"><code class=\"language-php\">&lt;?php\n$filename = \"example-zip-file.zip\";\nif (file_exists($filename)) { \/\/ adjust the below absolute file path according to the folder you have downloaded \/\/ the zip file \/\/ I have downloaded the zip file to the current folder $absoluteFilePath = __DIR__ . '\/' . $filename; header('Pragma: public'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Cache-Control: private', false); \/\/ content-type has to be defined according to the file extension (filetype) header('Content-Type: application\/zip'); header('Content-Disposition: attachment; filename=\"' . basename($filename) . '\";'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($absoluteFilePath)); readfile($absoluteFilePath); exit();\n}\n?&gt;\n<\/code><\/pre>\n<p>This file just has the links to trigger the function to <a href=\"https:\/\/phppot.com\/php\/create-zip-file-of-multiple-uploaded-files-using-php\/\">create a zip file containing the compressed archive of the directory<\/a>. Then, the action to download the output zip archive is called.<\/p>\n<p class=\"code-heading\">index.php<\/p>\n<pre class=\"prettyprint\"><code class=\"language-html\">&lt;div class='container'&gt; &lt;h2&gt;Create and Download Zip file using PHP&lt;\/h2&gt; &lt;p&gt; &lt;a href=\"create-zip-file.php\"&gt;Create Zip File&lt;\/a&gt; &lt;\/p&gt; &lt;p&gt; &lt;a href=\"download-zip-file.php\"&gt;Download Zip File&lt;\/a&gt; &lt;\/p&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<h2>Some methods of PHP ZipArchive class<\/h2>\n<p>We can do more operations by using the <a href=\"https:\/\/www.php.net\/manual\/en\/class.ziparchive.php\" target=\"_blank\" rel=\"noopener\">methods and properties of the PHP ZipArchive class<\/a>. This list of methods is provided by this PHP class.<\/p>\n<ol>\n<li>count() \u2013 used to get the number of files in the zip archive file.<\/li>\n<li>extractTo() \u2013 extracts the archive content.<\/li>\n<li>renameIndex() \u2013 rename a particular archive entry by index.<\/li>\n<li>replaceFile() \u2013 replace a file in the zip archive with a new file by specifying a new path.<\/li>\n<\/ol>\n<h3>ZipArchive methods used in this example<\/h3>\n<p>Some of the methods are used in this example listed below. These are frequently used methods of this class to work with this.<\/p>\n<ol>\n<li>open() \u2013 Open a zip archive file by specifying the .zip file name.<\/li>\n<li>addFile() \u2013 To add a file from the input directory to the zip archive.<\/li>\n<li>addEmptyDir() \u2013 adds an empty directory into the archive to load the subdirectory file of the input directory.<\/li>\n<li>close() \u2013 closes the active ZipArchive with the reference of the handle.<\/li>\n<\/ol>\n<p><a class=\"download\" href=\"https:\/\/phppot.com\/downloads\/php\/php-zip-create-download.zip\">Download<\/a><\/p>\n<p> <!-- #comments --> <\/p>\n<div class=\"related-articles\">\n<h2>Popular Articles<\/h2>\n<\/p><\/div>\n<p> <a href=\"https:\/\/phppot.com\/php\/php-create-zip-ziparchive-files-download\/#top\" class=\"top\">\u2191 Back to Top<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>by Vincy. Last modified on September 12th, 2022. Creating a zip from a folder full of files can be done in PHP using the ZipArchive class. This class instance creates a handle to read or write files to a compressed archive. This class includes several properties and methods to zip file archives. In this article, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":128051,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"class_list":["post-128050","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-updates"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=128050"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/128050\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/128051"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=128050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=128050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=128050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}