{"id":1140,"date":"2017-02-21T23:07:05","date_gmt":"2017-02-21T17:37:05","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=1140"},"modified":"2026-08-12T08:35:32","modified_gmt":"2026-08-12T03:05:32","slug":"php-zip-directory-automatically-download-using","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/","title":{"rendered":"Php zip a directory and automatically download"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p class=\"isSelectedEnd\">This guide explains how to use PHP to compress an entire directory into a ZIP file and automatically download the generated ZIP file through the browser. This is useful when users need to download multiple files or folders as a single archive.<\/p>\n<h3>Prerequisites<\/h3>\n<p class=\"isSelectedEnd\">Before implementing this functionality, ensure the following requirements are available:<\/p>\n<ul data-spread=\"false\">\n<li>PHP is installed and running on the server.<\/li>\n<li>The PHP <code dir=\"ltr\">ZipArchive<\/code> extension is enabled.<\/li>\n<li>The directory to be compressed exists and is readable by the PHP process.<\/li>\n<li>The web server has permission to create the ZIP file in the required location.<\/li>\n<li>Sufficient disk space is available for the ZIP archive.<\/li>\n<\/ul>\n<p class=\"isSelectedEnd\">You can verify whether the ZIP extension is enabled using:<\/p>\n<pre dir=\"ltr\"><code dir=\"ltr\">php -m | grep zip<\/code><\/pre>\n<h3><strong>Assumption value:<\/strong><\/h3>\n<pre>$sourecedir = 'images\/';\/\/this is an path of the folder name\n\n$zip_file = 'file.zip';\/\/this is an downloadable zip foler name<\/pre>\n<p>There two variable <strong>$sourcedir<\/strong>\u00a0and <strong>$zipfile, <\/strong>$sourcedir varible mention the folder and $zipfile mention downloadable folder name.<\/p>\n<p>First, initialise the archive object<\/p>\n<pre>$zip = new ZipArchive();\/\/ Initialize archive object&gt;\n$zip = new ZipArchive();\n$zip-&gt;open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);<\/pre>\n<p>Second, create a recursive directory iterator is download all files with subfoler<\/p>\n<pre>$files = new RecursiveIteratorIterator(\n    new RecursiveDirectoryIterator($rootPath)\n);\n<\/pre>\n<p>Third, use a foreach loop to add the file to the archive<\/p>\n<pre>foreach ($files as $name =&gt; $file)\n{\n    if (!$file-&gt;isDir())\n    {\n        $filePath = $file-&gt;getRealPath();\n        $relativePath = substr($filePath, strlen($rootPath) + 1);\n        $zip-&gt;addFile($filePath, $relativePath);\n    }\n}\n<\/pre>\n<p>Finally, close the zip and add zip header in PHP,read the file<\/p>\n<pre>$zip-&gt;close();\nheader('Content-Description: File Transfer');\nheader('Content-Type: application\/octet-stream');\nheader('Content-Disposition: attachment; filename='.basename($zip_file));\nheader('Content-Transfer-Encoding: binary');\nheader('Expires: 0');\nheader('Cache-Control: must-revalidate');\nheader('Pragma: public');\nheader('Content-Length: ' . filesize($zip_file));\nreadfile($zip_file);\n<\/pre>\n<p>If you follow all the steps and get the final code like this<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">&lt;?php\n$dir = 'target';\n$zip_file = 'file.zip';\n$rootPath = realpath($dir);\n$zip = new ZipArchive();\n$zip-&gt;open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);\n$files = new RecursiveIteratorIterator(\n    new RecursiveDirectoryIterator($rootPath)\n);\nforeach ($files as $name =&gt; $file)\n{\n    if (!$file-&gt;isDir())\n    {\n        $filePath = $file-&gt;getRealPath();\n        $relativePath = substr($filePath, strlen($rootPath) + 1);\n        $zip-&gt;addFile($filePath, $relativePath);\n    }\n}\n$zip-&gt;close();\nheader('Content-Description: File Transfer');\nheader('Content-Type: application\/octet-stream');\nheader('Content-Disposition: attachment; filename='.basename($zip_file));\nheader('Content-Transfer-Encoding: binary');\nheader('Expires: 0');\nheader('Cache-Control: must-revalidate');\nheader('Pragma: public');\nheader('Content-Length: ' . filesize($zip_file));\nreadfile($zip_file);\n\n?&gt;\n\n<\/pre>\n<h3>Conclusion<\/h3>\n<p>Using\u00a0PHP&#8217;s\u00a0&lt;code\u00a0dir=&#8221;ltr&#8221;&gt;ZipArchive\u00a0class\u00a0provides\u00a0a\u00a0simple\u00a0way\u00a0to\u00a0compress\u00a0a\u00a0directory\u00a0and\u00a0automatically\u00a0download\u00a0it\u00a0through\u00a0the\u00a0browser.\u00a0The\u00a0approach\u00a0recursively\u00a0includes\u00a0files\u00a0and\u00a0subdirectories,\u00a0creates\u00a0a\u00a0ZIP\u00a0archive,\u00a0sends\u00a0it\u00a0to\u00a0the\u00a0user,\u00a0and\u00a0removes\u00a0the\u00a0temporary\u00a0archive\u00a0after\u00a0the\u00a0download.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction This guide explains how to use PHP to compress an entire directory into a ZIP file and automatically download the generated ZIP file through the browser. This is useful when users need to download multiple files or folders as a single archive. Prerequisites Before implementing this functionality, ensure the&hellip; <a href=\"https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/\" class=\"more-link read-more\" rel=\"bookmark\">Continue Reading <span class=\"screen-reader-text\">Php zip a directory and automatically download<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[221],"tags":[],"class_list":{"0":"post-1140","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-php","7":"h-entry","9":"h-as-article"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Pheonix Solutions - We Empower Your Business Growth<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pheonix Solutions - We Empower Your Business Growth\" \/>\n<meta property=\"og:description\" content=\"Introduction This guide explains how to use PHP to compress an entire directory into a ZIP file and automatically download the generated ZIP file through the browser. This is useful when users need to download multiple files or folders as a single archive. Prerequisites Before implementing this functionality, ensure the&hellip; Continue Reading Php zip a directory and automatically download\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/\" \/>\n<meta property=\"og:site_name\" content=\"PHEONIXSOLUTIONS\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/PheonixSolutions-209942982759387\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-02-21T17:37:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-08-12T03:05:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2016\/09\/PX2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"3837\" \/>\n\t<meta property=\"og:image:height\" content=\"2540\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pheonixsolution\" \/>\n<meta name=\"twitter:site\" content=\"@pheonixsolution\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/php-zip-directory-automatically-download-using\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/php-zip-directory-automatically-download-using\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/0ffa33d73c869faec2d50e79c24e3503\"},\"headline\":\"Php zip a directory and automatically download\",\"datePublished\":\"2017-02-21T17:37:05+00:00\",\"dateModified\":\"2026-08-12T03:05:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/php-zip-directory-automatically-download-using\\\/\"},\"wordCount\":244,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#organization\"},\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/php-zip-directory-automatically-download-using\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/php-zip-directory-automatically-download-using\\\/\",\"url\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/php-zip-directory-automatically-download-using\\\/\",\"name\":\"Pheonix Solutions - We Empower Your Business Growth\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#website\"},\"datePublished\":\"2017-02-21T17:37:05+00:00\",\"dateModified\":\"2026-08-12T03:05:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/php-zip-directory-automatically-download-using\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/php-zip-directory-automatically-download-using\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/php-zip-directory-automatically-download-using\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Php zip a directory and automatically download\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/\",\"name\":\"Pheonix Solutions\",\"description\":\"We Empower Your Business Growth\",\"publisher\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#organization\",\"name\":\"PheonixSolutions\",\"url\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/12\\\/logo.png\",\"contentUrl\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/12\\\/logo.png\",\"width\":454,\"height\":300,\"caption\":\"PheonixSolutions\"},\"image\":{\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/PheonixSolutions-209942982759387\\\/\",\"https:\\\/\\\/x.com\\\/pheonixsolution\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/0ffa33d73c869faec2d50e79c24e3503\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/09bacc0294abee1322a23ab4bc6a0330dd4cb4df707dc9d0b0efeba6c109608b?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/09bacc0294abee1322a23ab4bc6a0330dd4cb4df707dc9d0b0efeba6c109608b?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/09bacc0294abee1322a23ab4bc6a0330dd4cb4df707dc9d0b0efeba6c109608b?s=96&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"http:\\\/\\\/pheonixsolutions.com\\\/blog\"],\"url\":\"https:\\\/\\\/pheonixsolutions.com\\\/blog\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pheonix Solutions - We Empower Your Business Growth","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/","og_locale":"en_US","og_type":"article","og_title":"Pheonix Solutions - We Empower Your Business Growth","og_description":"Introduction This guide explains how to use PHP to compress an entire directory into a ZIP file and automatically download the generated ZIP file through the browser. This is useful when users need to download multiple files or folders as a single archive. Prerequisites Before implementing this functionality, ensure the&hellip; Continue Reading Php zip a directory and automatically download","og_url":"https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/","og_site_name":"PHEONIXSOLUTIONS","article_publisher":"https:\/\/www.facebook.com\/PheonixSolutions-209942982759387\/","article_published_time":"2017-02-21T17:37:05+00:00","article_modified_time":"2026-08-12T03:05:32+00:00","og_image":[{"width":3837,"height":2540,"url":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2016\/09\/PX2.png","type":"image\/png"}],"author":"admin","twitter_card":"summary_large_image","twitter_creator":"@pheonixsolution","twitter_site":"@pheonixsolution","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/#article","isPartOf":{"@id":"https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/"},"author":{"name":"admin","@id":"https:\/\/pheonixsolutions.com\/blog\/#\/schema\/person\/0ffa33d73c869faec2d50e79c24e3503"},"headline":"Php zip a directory and automatically download","datePublished":"2017-02-21T17:37:05+00:00","dateModified":"2026-08-12T03:05:32+00:00","mainEntityOfPage":{"@id":"https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/"},"wordCount":244,"commentCount":0,"publisher":{"@id":"https:\/\/pheonixsolutions.com\/blog\/#organization"},"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/","url":"https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/","name":"Pheonix Solutions - We Empower Your Business Growth","isPartOf":{"@id":"https:\/\/pheonixsolutions.com\/blog\/#website"},"datePublished":"2017-02-21T17:37:05+00:00","dateModified":"2026-08-12T03:05:32+00:00","breadcrumb":{"@id":"https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/pheonixsolutions.com\/blog\/php-zip-directory-automatically-download-using\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/pheonixsolutions.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Php zip a directory and automatically download"}]},{"@type":"WebSite","@id":"https:\/\/pheonixsolutions.com\/blog\/#website","url":"https:\/\/pheonixsolutions.com\/blog\/","name":"Pheonix Solutions","description":"We Empower Your Business Growth","publisher":{"@id":"https:\/\/pheonixsolutions.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/pheonixsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/pheonixsolutions.com\/blog\/#organization","name":"PheonixSolutions","url":"https:\/\/pheonixsolutions.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/pheonixsolutions.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2016\/12\/logo.png","contentUrl":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2016\/12\/logo.png","width":454,"height":300,"caption":"PheonixSolutions"},"image":{"@id":"https:\/\/pheonixsolutions.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/PheonixSolutions-209942982759387\/","https:\/\/x.com\/pheonixsolution"]},{"@type":"Person","@id":"https:\/\/pheonixsolutions.com\/blog\/#\/schema\/person\/0ffa33d73c869faec2d50e79c24e3503","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/09bacc0294abee1322a23ab4bc6a0330dd4cb4df707dc9d0b0efeba6c109608b?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/09bacc0294abee1322a23ab4bc6a0330dd4cb4df707dc9d0b0efeba6c109608b?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/09bacc0294abee1322a23ab4bc6a0330dd4cb4df707dc9d0b0efeba6c109608b?s=96&r=g","caption":"admin"},"sameAs":["http:\/\/pheonixsolutions.com\/blog"],"url":"https:\/\/pheonixsolutions.com\/blog\/author\/admin\/"}]}},"jetpack_shortlink":"https:\/\/wp.me\/p7F4uM-io","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=1140"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1140\/revisions"}],"predecessor-version":[{"id":10778,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1140\/revisions\/10778"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}