{"id":1970,"date":"2015-07-29T22:17:29","date_gmt":"2015-07-30T05:17:29","guid":{"rendered":"https:\/\/www.virendrachandak.com\/techtalk\/?p=1970"},"modified":"2015-07-29T22:20:32","modified_gmt":"2015-07-30T05:20:32","slug":"how-to-create-a-zip-file-using-php","status":"publish","type":"post","link":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/","title":{"rendered":"How to create a zip file using PHP"},"content":{"rendered":"<p>Recently I had to write a script to create a zip file containing different files and folders. PHP has a ZipArchive class which can be used easily to create zip files. In this article we will see how to create zip files in PHP. We will create different zip files from different files and folders. <\/p>\n<ul>\n<li><a href=\"#new\">Create a new zip file<\/a><\/li>\n<li><a href=\"#overwrite\">Overwrite an existing zip file<\/a><\/li>\n<li><a href=\"#new_folder\">Create a new zip file and add files to be inside a folder<\/a><\/li>\n<li><a href=\"#change_folder\">Create a new zip file and move the files to be in different folders<\/a><\/li>\n<li><a href=\"#dir\">Create a zip file with all files from a directory<\/a><\/li>\n<li><a href=\"#files_dirs\">Add multiple files and directories to a zip file<\/a><\/li>\n<\/ul>\n<p><!--more--><\/p>\n<div id=\"new\">\n<h3>Create a new zip file<\/h3>\n<p>\tThe following code will open a zip file <code>test_new.zip<\/code> and add a few files into it.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\t$zip = new ZipArchive;\r\n\tif ($zip-&gt;open('test_new.zip', ZipArchive::CREATE) === TRUE)\r\n\t{\r\n\t\t\/\/ Add files to the zip file\r\n\t\t$zip-&gt;addFile('test.txt');\r\n\t\t$zip-&gt;addFile('test.pdf');\r\n\r\n\t\t\/\/ Add random.txt file to zip and rename it to newfile.txt\r\n\t\t$zip-&gt;addFile('random.txt', 'newfile.txt');\r\n\r\n\t\t\/\/ Add a file new.txt file to zip using the text specified\r\n\t\t$zip-&gt;addFromString('new.txt', 'text to be added to the new.txt file');\r\n\r\n\t\t\/\/ All files are added, so close the zip file.\r\n\t\t$zip-&gt;close();\r\n\t}<\/pre>\n<h5>Explanation of code<\/h5>\n<ul>\n<li>Line 1 creates an object of the ZipArchive class<\/li>\n<li>Line 2 opens a file with filename as <code>test_new.zip<\/code> so that we can add files to it. The flag <code>ZipArchive::CREATE<\/code> specifies that we want to create a new zip file<\/li>\n<li>Lines 5 &#038; 6 are used to add files to the zip file<\/li>\n<li>Line 9 is used to add a file with name <code>random.txt<\/code> to the zip file and rename it in the zipfile as <code>newfile.txt<\/code><\/li>\n<li>Line 12 is used to add a new file <code>new.txt<\/code> with contents of the file as &#8216;text to be added to the new.txt file&#8217;<\/li>\n<li>Line 15 closes and saves the changes to the zip file<\/li>\n<\/ul>\n<p>\tNote: Sometimes there can be issues when using relative paths for files. If there are any issues using paths then we can also use absolute paths for files\n<\/p><\/div>\n<div id=\"overwrite\">\n<h3>Overwrite an existing zip file<\/h3>\n<p>\tIf you want to overwrite an existing zip file then we can use code similar to following. The flag <code>ZipArchive::OVERWRITE<\/code> overwrites the existing zip file.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\t$zip = new ZipArchive;\r\n\tif ($zip-&gt;open('test_overwrite.zip', ZipArchive::OVERWRITE) === TRUE)\r\n\t{\r\n\t\t\/\/ Add file to the zip file\r\n\t\t$zip-&gt;addFile('test.txt');\r\n\t\t$zip-&gt;addFile('test.pdf');\r\n\r\n\t\t\/\/ All files are added, so close the zip file.\r\n\t\t$zip-&gt;close();\r\n\t}<\/pre>\n<h5>Explanation of code<\/h5>\n<ul>\n<li>This code will create a file <code>test_overwrite.zip<\/code> if it already exists the file will be overwritten with this new file<\/li>\n<\/ul>\n<\/div>\n<div id=\"new_folder\">\n<h3>Create a new zip file and add files to be inside a folder<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\t$zip = new ZipArchive;\r\n\tif ($zip-&gt;open('test_folder.zip', ZipArchive::CREATE) === TRUE)\r\n\t{\r\n\t\t\/\/ Add files to the zip file inside demo_folder\r\n\t\t$zip-&gt;addFile('text.txt', 'demo_folder\/test.txt');\r\n\t\t$zip-&gt;addFile('test.pdf', 'demo_folder\/test.pdf');\r\n\r\n\t\t\/\/ Add random.txt file to zip and rename it to newfile.txt and store in demo_folder\r\n\t\t$zip-&gt;addFile('random.txt', 'demo_folder\/newfile.txt');\r\n\r\n\t\t\/\/ Add a file demo_folder\/new.txt file to zip using the text specified\r\n\t\t$zip-&gt;addFromString('demo_folder\/new.txt', 'text to be added to the new.txt file');\r\n\r\n\t\t\/\/ All files are added, so close the zip file.\r\n\t\t$zip-&gt;close();\r\n\t}<\/pre>\n<h5>Explanation of code<\/h5>\n<ul>\n<li>The above code will add different files inside the zip file to be inside a folder <code>demo_folder<\/code><\/li>\n<li>The 2nd parameter to addfile function can be used to store the file in a new folder<\/li>\n<li>The 1st parameter in the addFromString function can be used to store the file in a new folder<\/li>\n<\/ul>\n<\/div>\n<div id=\"change_folder\">\n<h3>Create a new zip file and move the files to be in different folders<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\t$zip = new ZipArchive;\r\n\tif ($zip-&gt;open('test_folder_change.zip', ZipArchive::CREATE) === TRUE)\r\n\t{\r\n\t\t\/\/ Add files to the zip file\r\n\t\t$zip-&gt;addFile('text.txt', 'demo_folder\/test.txt');\r\n\t\t$zip-&gt;addFile('test.pdf', 'demo_folder1\/test.pdf');\r\n\r\n\t\t\/\/ All files are added, so close the zip file.\r\n\t\t$zip-&gt;close();\r\n\t}<\/pre>\n<h5>Explanation of code<\/h5>\n<ul>\n<li>We store the file test.txt into demo_folder and test.pdf into demo_folder1<\/li>\n<\/ul>\n<\/div>\n<div id=\"dir\">\n<h3>Create a zip file with all files from a directory<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\t$zip = new ZipArchive;\r\n\tif ($zip-&gt;open('test_dir.zip', ZipArchive::OVERWRITE) === TRUE)\r\n\t{\r\n\t\tif ($handle = opendir('demo_folder'))\r\n\t\t{\r\n\t\t\t\/\/ Add all files inside the directory\r\n\t\t\twhile (false !== ($entry = readdir($handle)))\r\n\t\t\t{\r\n\t\t\t\tif ($entry != &quot;.&quot; &amp;&amp; $entry != &quot;..&quot; &amp;&amp; !is_dir('demo_folder\/' . $entry))\r\n\t\t\t\t{\r\n\t\t\t\t\t$zip-&gt;addFile('demo_folder\/' . $entry);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tclosedir($handle);\r\n\t\t}\r\n\r\n\t\t$zip-&gt;close();\r\n\t}<\/pre>\n<h5>Explanation of code<\/h5>\n<ul>\n<li>Lines 5-16 opens a directory and creates a zip file with all files within that directory<\/li>\n<li>Line 5 opens the directory<\/li>\n<li>Line 7 gets the name of each file in the dir<\/li>\n<li>Line 9 skips the &#8220;.&#8221; and &#8220;..&#8221; and any other directories<\/li>\n<li>Line 11 adds the file into the zip file<\/li>\n<li>Line 14 closes the directory<\/li>\n<li>Line 17 closes the zip file<\/li>\n<\/ul>\n<\/div>\n<div id=\"files_dirs\">\n<h3>Add multiple files and directories to a zip file<\/h3>\n<p>\tThe following code will add different folders and files in those directories into a zip file<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\t$zip = new ZipArchive;\r\n\tif ($zip-&gt;open('test_files_dirs.zip', ZipArchive::OVERWRITE) === TRUE)\r\n\t{\r\n\t\t\/\/ Add directory1\r\n\t\tif ($handle = opendir('demo_folder\/directory1\/'))\r\n\t\t{\r\n\t\t\twhile (false !== ($entry = readdir($handle)))\r\n\t\t\t{\r\n\t\t\t\tif ($entry != &quot;.&quot; &amp;&amp; $entry != &quot;..&quot;)\r\n\t\t\t\t{\r\n\t\t\t\t\t$zip-&gt;addFile('demo_folder\/directory1\/' . $entry);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tclosedir($handle);\r\n\t\t}\r\n\r\n\t\t\/\/ Add directory2\r\n\t\tif ($handle = opendir('demo_folder\/directory2\/'))\r\n\t\t{\r\n\t\t\twhile (false !== ($entry = readdir($handle)))\r\n\t\t\t{\r\n\t\t\t\tif ($entry != &quot;.&quot; &amp;&amp; $entry != &quot;..&quot;)\r\n\t\t\t\t{\r\n\t\t\t\t\t$zip-&gt;addFile('demo_folder\/directory2\/' . $entry);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tclosedir($handle);\r\n\t\t}\r\n\r\n\t\t\/\/ Add directory3\r\n\t\tif ($handle = opendir('demo_folder\/directory3\/'))\r\n\t\t{\r\n\t\t\twhile (false !== ($entry = readdir($handle)))\r\n\t\t\t{\r\n\t\t\t\tif ($entry != &quot;.&quot; &amp;&amp; $entry != &quot;..&quot;)\r\n\t\t\t\t{\r\n\t\t\t\t\t$zip-&gt;addFile('demo_folder\/directory3\/' . $entry);\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tclosedir($handle);\r\n\t\t}\r\n\r\n\t\t\/\/ Add more files\r\n\t\t$zip-&gt;addFile('demo_folder\/index.txt');\r\n\r\n\t\t$zip-&gt;close();\r\n\t}<\/pre>\n<h5>Explanation of code<\/h5>\n<ul>\n<li>Lines 5-41 adds all the files inside the directories 1,2 and 3 into the zip file inside the respective directories<\/li>\n<li>Line 44 adds &#8220;index.txt&#8221; file to the zip file<\/li>\n<\/ul>\n<\/div>\n<div>\n<a id=\"demo_link\" data-title=\"View Demo\" href=\"https:\/\/www.virendrachandak.com\/demos\/how-to-create-a-zip-file-using-php.php\"><i class=\"icon fa fa-camera-retro\"><\/i> View Demo<\/a>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Recently I had to write a script to create a zip file containing different files and folders. PHP has a ZipArchive class which can be used easily to create zip files. In this article we will see how to create zip files in PHP. We will create different zip files from different files and folders. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[143],"tags":[124,146],"class_list":["post-1970","post","type-post","status-publish","format-standard","hentry","category-php","tag-snippets","tag-zip"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to create a zip file using PHP - Virendra&#039;s TechTalk<\/title>\n<meta name=\"description\" content=\"Recently I had to write a script to create a zip file containing different files and folders. PHP has a ZipArchive class which can be used easily to\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to create a zip file using PHP - Virendra&#039;s TechTalk\" \/>\n<meta property=\"og:description\" content=\"Recently I had to write a script to create a zip file containing different files and folders. PHP has a ZipArchive class which can be used easily to\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Virendra&#039;s TechTalk\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/virendrachandak\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/virendrachandak\" \/>\n<meta property=\"article:published_time\" content=\"2015-07-30T05:17:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-07-30T05:20:32+00:00\" \/>\n<meta name=\"author\" content=\"Virendra Chandak\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@virendrachandak\" \/>\n<meta name=\"twitter:site\" content=\"@virendrachandak\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virendra Chandak\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-create-a-zip-file-using-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-create-a-zip-file-using-php\\\/\"},\"author\":{\"name\":\"Virendra Chandak\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\"},\"headline\":\"How to create a zip file using PHP\",\"datePublished\":\"2015-07-30T05:17:29+00:00\",\"dateModified\":\"2015-07-30T05:20:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-create-a-zip-file-using-php\\\/\"},\"wordCount\":1028,\"commentCount\":5,\"publisher\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\"},\"keywords\":[\"snippets\",\"zip\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-create-a-zip-file-using-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-create-a-zip-file-using-php\\\/\",\"url\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-create-a-zip-file-using-php\\\/\",\"name\":\"How to create a zip file using PHP - Virendra&#039;s TechTalk\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#website\"},\"datePublished\":\"2015-07-30T05:17:29+00:00\",\"dateModified\":\"2015-07-30T05:20:32+00:00\",\"description\":\"Recently I had to write a script to create a zip file containing different files and folders. PHP has a ZipArchive class which can be used easily to\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-create-a-zip-file-using-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-create-a-zip-file-using-php\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/how-to-create-a-zip-file-using-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"TechTalk\",\"item\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/category\\\/php\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to create a zip file using PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#website\",\"url\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/\",\"name\":\"Virendra's TechTalk\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/63f7ffa1ea125e32af9618d188349e17\",\"name\":\"Virendra Chandak\",\"logo\":{\"@id\":\"https:\\\/\\\/www.virendrachandak.com\\\/techtalk\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.virendrachandak.com\",\"https:\\\/\\\/www.facebook.com\\\/virendrachandak\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/virendrachandak\\\/\",\"https:\\\/\\\/x.com\\\/virendrachandak\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to create a zip file using PHP - Virendra&#039;s TechTalk","description":"Recently I had to write a script to create a zip file containing different files and folders. PHP has a ZipArchive class which can be used easily to","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:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/","og_locale":"en_US","og_type":"article","og_title":"How to create a zip file using PHP - Virendra&#039;s TechTalk","og_description":"Recently I had to write a script to create a zip file containing different files and folders. PHP has a ZipArchive class which can be used easily to","og_url":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/","og_site_name":"Virendra&#039;s TechTalk","article_publisher":"https:\/\/www.facebook.com\/virendrachandak","article_author":"https:\/\/www.facebook.com\/virendrachandak","article_published_time":"2015-07-30T05:17:29+00:00","article_modified_time":"2015-07-30T05:20:32+00:00","author":"Virendra Chandak","twitter_card":"summary_large_image","twitter_creator":"@virendrachandak","twitter_site":"@virendrachandak","twitter_misc":{"Written by":"Virendra Chandak","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/#article","isPartOf":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/"},"author":{"name":"Virendra Chandak","@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"headline":"How to create a zip file using PHP","datePublished":"2015-07-30T05:17:29+00:00","dateModified":"2015-07-30T05:20:32+00:00","mainEntityOfPage":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/"},"wordCount":1028,"commentCount":5,"publisher":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"keywords":["snippets","zip"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/","url":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/","name":"How to create a zip file using PHP - Virendra&#039;s TechTalk","isPartOf":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#website"},"datePublished":"2015-07-30T05:17:29+00:00","dateModified":"2015-07-30T05:20:32+00:00","description":"Recently I had to write a script to create a zip file containing different files and folders. PHP has a ZipArchive class which can be used easily to","breadcrumb":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-create-a-zip-file-using-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"TechTalk","item":"https:\/\/www.virendrachandak.com\/techtalk\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},{"@type":"ListItem","position":3,"name":"How to create a zip file using PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.virendrachandak.com\/techtalk\/#website","url":"https:\/\/www.virendrachandak.com\/techtalk\/","name":"Virendra's TechTalk","description":"","publisher":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.virendrachandak.com\/techtalk\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/63f7ffa1ea125e32af9618d188349e17","name":"Virendra Chandak","logo":{"@id":"https:\/\/www.virendrachandak.com\/techtalk\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/www.virendrachandak.com","https:\/\/www.facebook.com\/virendrachandak","https:\/\/www.linkedin.com\/in\/virendrachandak\/","https:\/\/x.com\/virendrachandak"]}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2vTtQ-vM","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":224,"url":"https:\/\/www.virendrachandak.com\/techtalk\/htaccess-tips\/","url_meta":{"origin":1970,"position":0},"title":".htaccess tips","author":"Virendra Chandak","date":"October 2, 2011","format":false,"excerpt":"Topics Covered: What is .htaccess file? When to use .htaccess file? Disadvantage of using .htaccess file Authentication using .htaccess file Custom Error Document Allow\/Disallow Directory Listings What is .htaccess file? .htaccess file is used on Apache Web Server to make configuration changes on per-directory basis. This file contains the configuration\u2026","rel":"","context":"In &quot;Server Configuration&quot;","block_context":{"text":"Server Configuration","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/server-configuration\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":255,"url":"https:\/\/www.virendrachandak.com\/techtalk\/more-htaccess-tips\/","url_meta":{"origin":1970,"position":1},"title":"more .htaccess tips","author":"Virendra Chandak","date":"October 16, 2011","format":false,"excerpt":"In my previous post .htaccess tips I had started with what is .htaccess file and some things that can be done using it. In this post I'll cover more about .htaccess files. Topics Covered: Directory index file Redirection Preferred domain (www or non-www) Redirect old site to new site Redirect\u2026","rel":"","context":"In &quot;Server Configuration&quot;","block_context":{"text":"Server Configuration","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/server-configuration\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2392,"url":"https:\/\/www.virendrachandak.com\/techtalk\/how-to-find-laravel-version\/","url_meta":{"origin":1970,"position":2},"title":"How to Check Laravel Version through CLI \/ command prompt and file","author":"Virendra Chandak","date":"May 31, 2020","format":false,"excerpt":"There are 2 easy ways to find out what version of laravel you are using. They are by running php artisian commands on CLI or checking the files. Method 1: Check Laravel Version using CLI \/ Command Prompt [sourcecode gutter=\"false\" lang=\"powershell\"] $ php artisan --version Laravel Framework 7.12.0 [\/sourcecode] The\u2026","rel":"","context":"In &quot;Laravel&quot;","block_context":{"text":"Laravel","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/laravel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1121,"url":"https:\/\/www.virendrachandak.com\/techtalk\/understanding-favicon\/","url_meta":{"origin":1970,"position":3},"title":"Understanding favicon","author":"Virendra Chandak","date":"August 6, 2014","format":false,"excerpt":"A favicon (short for \"favorites icon\") is a small image associated with a website intended to be used when you bookmark a page. Web browsers also use them in the URL bar, on tabs, etc to help the users identify a website. It is typically a 16x16 pixels square icon\u2026","rel":"","context":"In &quot;Glossary\/Definitions&quot;","block_context":{"text":"Glossary\/Definitions","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/glossarydefinitions\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1968,"url":"https:\/\/www.virendrachandak.com\/techtalk\/creating-csv-file-using-php-and-mysql\/","url_meta":{"origin":1970,"position":4},"title":"How to create CSV file using PHP","author":"Virendra Chandak","date":"April 19, 2015","format":false,"excerpt":"CSV (comma-separated values) is one of the most popular methods for transferring tabular data between applications. Lot of applications want to export data in a CSV file. In this article we will see how we can create CSV file using PHP. We will also see how to automatically download the\u2026","rel":"","context":"In &quot;PHP&quot;","block_context":{"text":"PHP","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":598,"url":"https:\/\/www.virendrachandak.com\/techtalk\/notepad-keyboard-shortcuts\/","url_meta":{"origin":1970,"position":5},"title":"Notepad++ Keyboard Shortcuts","author":"Virendra Chandak","date":"December 10, 2011","format":false,"excerpt":"I have been using Notepad++ everyday for coding at home and work for past 2-3 years. I don't remember using any other editor during this entire period. I like this editor very much due to various features like syntax highlighting, collapse\/expand functionality, ability to open 2 workspaces in same window,\u2026","rel":"","context":"In &quot;Tools&quot;","block_context":{"text":"Tools","link":"https:\/\/www.virendrachandak.com\/techtalk\/category\/tools\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts\/1970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/comments?post=1970"}],"version-history":[{"count":0,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/posts\/1970\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/media?parent=1970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/categories?post=1970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.virendrachandak.com\/techtalk\/wp-json\/wp\/v2\/tags?post=1970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}