{"id":58451,"date":"2016-09-03T07:41:03","date_gmt":"2016-09-03T07:41:03","guid":{"rendered":"http:\/\/webkul.com\/blog\/?p=58451"},"modified":"2016-09-03T07:41:03","modified_gmt":"2016-09-03T07:41:03","slug":"adds_file_to_zip_and_download_in_php","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/","title":{"rendered":"How to add files to a ZIP and download it in PHP"},"content":{"rendered":"<p><strong>Hello,<\/strong><\/p>\n<p>Here we learn, how to add files to a zip and download that zip in PHP.<\/p>\n<p><strong>For Example:<\/strong><\/p>\n<p>We have an array of files:<\/p>\n<pre class=\"brush:php\">$files =array(\"test.txt\", \"demo.jpg\", \"hello.txt\", \"example.txt\");\/\/ array of files\r\n\r\n $zip = new ZipArchive();\/\/Create an object of ZipArchive class\r\n  \r\n $zipname=\"example_zip\";\/\/ give zip file name \r\n\r\n $zip-&gt;open($zipname, ZipArchive::CREATE); \/\/example_zip zip file created \r\n \r\n foreach($files as $key =&gt;$file){\r\n     $zip-&gt;addFile($file);\/\/add each file into example_zip zip file\r\n }\r\n \r\n $zip-&gt;close();\/\/ zip file with files created successful now close it\r\n\r\n<strong>Download Zip File<\/strong>\r\n  header('Content-Type: application\/zip');\r\n  header('Content-disposition: attachment; filename='.$zipname);\r\n  header('Content-Length: ' . filesize($zipname));\r\n  readfile($zipname);<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Thanks<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, Here we learn, how to add files to a zip and download that zip in PHP. For Example: We have an array of files: $files =array(&#8220;test.txt&#8221;, &#8220;demo.jpg&#8221;, &#8220;hello.txt&#8221;, &#8220;example.txt&#8221;);\/\/ array of files $zip = new ZipArchive();\/\/Create an object of ZipArchive class $zipname=&#8221;example_zip&#8221;;\/\/ give zip file name $zip-&gt;open($zipname, ZipArchive::CREATE); \/\/example_zip zip file created foreach($files as <a href=\"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":93,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[673,3565,3564],"class_list":["post-58451","post","type-post","status-publish","format-standard","hentry","category-php","tag-download","tag-file","tag-zip"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to add files to a ZIP and download it in PHP - Webkul Blog<\/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:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to add files to a ZIP and download it in PHP - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Hello, Here we learn, how to add files to a zip and download that zip in PHP. For Example: We have an array of files: $files =array(&quot;test.txt&quot;, &quot;demo.jpg&quot;, &quot;hello.txt&quot;, &quot;example.txt&quot;);\/\/ array of files $zip = new ZipArchive();\/\/Create an object of ZipArchive class $zipname=&quot;example_zip&quot;;\/\/ give zip file name $zip-&gt;open($zipname, ZipArchive::CREATE); \/\/example_zip zip file created foreach($files as [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2016-09-03T07:41:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Prinsika Pandey\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prinsika Pandey\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/\"},\"author\":{\"name\":\"Prinsika Pandey\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/001f341c71ba8884a38d75e18d66670c\"},\"headline\":\"How to add files to a ZIP and download it in PHP\",\"datePublished\":\"2016-09-03T07:41:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/\"},\"wordCount\":40,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"download\",\"file\",\"zip\"],\"articleSection\":[\"php\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/\",\"url\":\"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/\",\"name\":\"How to add files to a ZIP and download it in PHP - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2016-09-03T07:41:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to add files to a ZIP and download it in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/001f341c71ba8884a38d75e18d66670c\",\"name\":\"Prinsika Pandey\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/37b533b469af9a48d2a44b38cabdeabc5524726e70ad6c18d522b420acdd83ba?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/37b533b469af9a48d2a44b38cabdeabc5524726e70ad6c18d522b420acdd83ba?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Prinsika Pandey\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/prinsika-pandey637\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to add files to a ZIP and download it in PHP - Webkul Blog","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:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/","og_locale":"en_US","og_type":"article","og_title":"How to add files to a ZIP and download it in PHP - Webkul Blog","og_description":"Hello, Here we learn, how to add files to a zip and download that zip in PHP. For Example: We have an array of files: $files =array(\"test.txt\", \"demo.jpg\", \"hello.txt\", \"example.txt\");\/\/ array of files $zip = new ZipArchive();\/\/Create an object of ZipArchive class $zipname=\"example_zip\";\/\/ give zip file name $zip-&gt;open($zipname, ZipArchive::CREATE); \/\/example_zip zip file created foreach($files as [...]","og_url":"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2016-09-03T07:41:03+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Prinsika Pandey","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Prinsika Pandey","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/"},"author":{"name":"Prinsika Pandey","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/001f341c71ba8884a38d75e18d66670c"},"headline":"How to add files to a ZIP and download it in PHP","datePublished":"2016-09-03T07:41:03+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/"},"wordCount":40,"commentCount":0,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["download","file","zip"],"articleSection":["php"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/","url":"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/","name":"How to add files to a ZIP and download it in PHP - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2016-09-03T07:41:03+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/adds_file_to_zip_and_download_in_php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to add files to a ZIP and download it in PHP"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/001f341c71ba8884a38d75e18d66670c","name":"Prinsika Pandey","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/37b533b469af9a48d2a44b38cabdeabc5524726e70ad6c18d522b420acdd83ba?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/37b533b469af9a48d2a44b38cabdeabc5524726e70ad6c18d522b420acdd83ba?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Prinsika Pandey"},"url":"https:\/\/webkul.com\/blog\/author\/prinsika-pandey637\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/58451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/93"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=58451"}],"version-history":[{"count":1,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/58451\/revisions"}],"predecessor-version":[{"id":58456,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/58451\/revisions\/58456"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=58451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=58451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=58451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}