{"id":331,"date":"2022-11-21T00:00:34","date_gmt":"2022-11-21T00:00:34","guid":{"rendered":"https:\/\/code-boxx.com\/?p=331"},"modified":"2023-11-15T02:56:50","modified_gmt":"2023-11-15T02:56:50","slug":"read-zip-file-php","status":"publish","type":"post","link":"https:\/\/code-boxx.com\/read-zip-file-php\/","title":{"rendered":"Read Zip Files In PHP (Simple Examples)"},"content":{"rendered":"<p>Welcome to a quick tutorial on how to read zip files in PHP. So the old zip functions are deprecated in PHP 8, how do we read files using Zip Archive then? This has caused some confusion, but it is actually very straightforward.<\/p>\n<div class=\"sec-head answer\">\n<p>To read zip files in PHP, we can use <code>statIndex()<\/code> to get the file statistics, and <code>getFromName()<\/code> to directly read a file.<\/p>\n<ul>\n<li><code>$zip = new ZipArchive;<\/code><\/li>\n<li><code>$zip-&gt;open(\"FILE.ZIP\", ZipArchive::RDONLY);<\/code><\/li>\n<li><code>$entries = $zip-&gt;count();<\/code><\/li>\n<li><code>for ($i=0; $i&lt;$entries; $i++) { $stat = $zip-&gt;statIndex($i); }<\/code><\/li>\n<li><code>$content = $zip-&gt;getFromName(\"FILE-IN-ZIP.TXT\");<\/code><\/li>\n<li><code>$zip-&gt;close();<\/code><\/li>\n<\/ul>\n<\/div>\n<p>That should cover the basics, but if you need more concrete examples &#8211; Read on!<\/p>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-101\"><\/div>\n<p>&nbsp;<\/p>\n<h2 class=\"in-head-d\">TABLE OF CONTENTS<\/h2>\n<div class=\"toc\">\n<div><a href=\"#sec-download\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-download.svg\" width=\"36\" height=\"36\" \/>Download &amp; Notes<\/a><\/div>\n<div><a href=\"#sec-read\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-search.svg\" width=\"36\" height=\"36\" \/>PHP Read Zip<\/a><\/div>\n<div><a href=\"#sec-extra\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-plus-square.svg\" width=\"36\" height=\"36\" \/>Extras<\/a><\/div>\n<div><a href=\"#sec-end\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/code-boxx.com\/wp-content\/uploads\/2019\/08\/ico-thumbs-up.svg\" width=\"36\" height=\"36\" \/>The End<\/a><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div id=\"sec-download\" class=\"sec-head\" style=\"background: #ffe8de;\">\n<h2 class=\"in-head-d\">DOWNLOAD &amp; NOTES<\/h2>\n<p>Here is the download link to the example code, so you don&#8217;t have to copy-paste everything.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">EXAMPLE CODE DOWNLOAD<\/span><\/h3>\n<p><a href=\"https:\/\/gist.github.com\/code-boxx\/28f173bf42aa052ed04bb6e8bb7a95ef\" target=\"_blank\" rel=\"noopener noreferrer\">Source code on GitHub Gist<\/a><\/p>\n<p>Just click on &#8220;download zip&#8221; or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.<\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">SORRY FOR THE ADS...<\/span><\/h3>\r\n<p>But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a \"paid scripts\" business, and I don't \"block people with Adblock\". Every little bit of support helps.<\/p>\r\n\r\n<p><a class=\"ast-button\" href=\"https:\/\/www.paypal.com\/paypalme\/wstoh\/5\" target=\"_blank\" rel=\"nofollow noopener\">Buy Me A Coffee<\/a> <a class=\"ast-button\" href=\"https:\/\/payhip.com\/codeboxx\" target=\"_blank\" rel=\"nofollow noopener\">Code Boxx eBooks<\/a><\/p>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-102\"><\/div>\n<p>&nbsp;<\/p>\n<div id=\"sec-read\" class=\"sec-head\" style=\"background: #e0f3ff;\">\n<h2 class=\"in-head-d\">PHP READ ZIP FILE<\/h2>\n<p>All right, let us now get into the examples of how to read a zip file in PHP.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">1) READ FILE LIST OR INFORMATION<\/span><\/h3>\n<div class=\"ref-head\">1-info.php<\/div>\n<pre><code>&lt;?php\r\n\/\/ (A) OPEN ZIP FILE\r\n$file = \"test.zip\";\r\n$zip = new ZipArchive;\r\nif ($zip-&gt;open($file, ZipArchive::RDONLY) !== true) {\r\n  exit(\"Failed to open $file\");\r\n}\r\n\r\n\/\/ (B) LOOP THROUGH EVERY FILE &amp; FOLDER - DISPLAY INFORMATION\r\n$total = $zip-&gt;count();\r\nfor ($i=0; $i&lt;$total; $i++) {\r\n  print_r($zip-&gt;statIndex($i));\r\n}\r\n\r\n\/\/ (C) CLOSE ZIP\r\n$zip-&gt;close();<\/code><\/pre>\n<p>This should be pretty self-explanatory, and how Zip Archive works is actually somewhat like an array &#8211; Each file or folder inside the zip archive will have an index number. To get a whole list of files\/folders inside the archive, we can simply use a <code>for<\/code> loop to run through them and <code>statIndex()<\/code> to fetch the information.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-103\"><\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">2) READ FILE IN ZIP ARCHIVE<\/span><\/h3>\n<div class=\"ref-head\">2-read.php<\/div>\n<pre><code>&lt;?php\r\n\/\/ (A) OPEN ZIP FILE\r\n$file = \"test.zip\";\r\n$zip = new ZipArchive;\r\nif ($zip-&gt;open($file, ZipArchive::RDONLY) !== true) {\r\n  exit(\"Failed to open $file\");\r\n}\r\n\r\n\/\/ (B) DIRECTLY READ FILE USING INDEX\r\n$content = $zip-&gt;getFromIndex(0);\r\necho \"$content&lt;br&gt;\";\r\n\r\n\/\/ (C) OR USE GET FROM FILE NAME\r\n$content = $zip-&gt;getFromName(\"second.txt\");\r\necho \"$content&lt;br&gt;\";\r\n\r\n\/\/ (D) CLOSE ZIP\r\n$zip-&gt;close();<\/code><\/pre>\n<p>To read the contents of a file inside a zip archive, we can either use <code>getFromIndex()<\/code> or <code>getFromName()<\/code>. But there will be a problem dealing with large files in this one. While both functions accept a second parameter to restrict the number of bytes read, there is just no way to open it as a file stream &#8211; The best way to deal with large files is still to unzip it and use <code>fgets()<\/code> instead.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-106\"><\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">3) LEGACY ZIP READ<\/span><\/h3>\n<div class=\"ref-head\">3-legacy.php<\/div>\n<pre><code>&lt;?php\r\n\/\/ (A) OPEN ZIP FILE\r\n$file = \"test.zip\";\r\n$zip = zip_open($file);\r\nif (is_numeric($zip) || $zip===false) {\r\n  exit(\"Failed to open $file\");\r\n}\r\n\r\n\/\/ (B) READ FILES\r\nwhile ($entry = zip_read($zip)) {\r\n  \/\/ (B1) GET FILE NAME &amp; SIZE\r\n  $name = zip_entry_name($entry);\r\n  $size = zip_entry_filesize($entry);\r\n  echo \"$name - $size bytes&lt;br&gt;\";\r\n\r\n  \/\/ (B2) READ FILE DIRECTLY\r\n  if ($name == \"first.txt\") {\r\n    if (zip_entry_open($zip, $entry)) {\r\n      $contents = zip_entry_read($entry);\r\n      echo \"$contents&lt;br&gt;\";\r\n      zip_entry_close($entry);\r\n    } else { echo \"Cannot open first.txt\"; }\r\n  }\r\n}\r\nzip_close($zip);<\/code><\/pre>\n<p>Yes, the old zip functions are deprecated in PHP 8 (they still work but will eventually disappear). I shall not explain too much on this example but leave it here as &#8220;legacy support&#8221;&#8230; Do not use this method anymore, unless you have to support the older PHP versions.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"ezoic-pub-ad-placeholder-107\"><\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">EXTRA) HOW TO UNZIP FILE<\/span><\/h3>\n<pre><code>&lt;?php\r\n$zip\u00a0=\u00a0new\u00a0ZipArchive;\r\nif ($zip-&gt;open(\"TEST.ZIP\") === TRUE) {\r\n \u00a0$zip-&gt;extractTo(\"DESTINATION\/FOLDER\/\");\r\n \u00a0$zip-&gt;close();\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<div id=\"sec-extra\" class=\"sec-head\" style=\"background: #f8d5ff;\">\n<h2 class=\"in-head-d\">EXTRAS<\/h2>\n<p>That&#8217;s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #008000;\">SUMMARY &amp; REFERENCES<\/span><\/h3>\n<table class=\"zebra\">\n<tbody>\n<tr>\n<td><strong>Function<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<td><strong>Link<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>$zip-&gt;open(FILE, MODE)<\/code><\/td>\n<td>Open a zip archive.<\/td>\n<td><a href=\"https:\/\/www.php.net\/manual\/en\/ziparchive.open.php\" target=\"_blank\" rel=\"noopener\">Click Here<\/a><\/td>\n<\/tr>\n<tr>\n<td><code>$zip-&gt;count()<\/code><\/td>\n<td>Get a count of the total number of files\/folders.<\/td>\n<td><a href=\"https:\/\/www.php.net\/manual\/en\/ziparchive.count.php\" target=\"_blank\" rel=\"noopener\">Click Here<\/a><\/td>\n<\/tr>\n<tr>\n<td><code>$zip-&gt;statIndex(INDEX)<\/code><\/td>\n<td>Get the file\/folder information at the specified index slot.<\/td>\n<td><a href=\"https:\/\/www.php.net\/manual\/en\/ziparchive.statindex.php\" target=\"_blank\" rel=\"noopener\">Click Here<\/a><\/td>\n<\/tr>\n<tr>\n<td><code>$zip-&gt;getFromIndex(INDEX)<\/code><\/td>\n<td>Read the file\/folder content at the specified index slot.<\/td>\n<td><a href=\"https:\/\/www.php.net\/manual\/en\/ziparchive.getfromindex.php\" target=\"_blank\" rel=\"noopener\">Click Here<\/a><\/td>\n<\/tr>\n<tr>\n<td><code>$zip-&gt;getFromName(FILE NAME)<\/code><\/td>\n<td>Read the file\/folder content of the specified file name.<\/td>\n<td><a href=\"https:\/\/www.php.net\/manual\/en\/ziparchive.getfromname.php\" target=\"_blank\" rel=\"noopener\">Click Here<\/a><\/td>\n<\/tr>\n<tr>\n<td><code>$zip-&gt;close()<\/code><\/td>\n<td>Close the zip archive.<\/td>\n<td><a href=\"https:\/\/www.php.net\/manual\/en\/ziparchive.close.php\" target=\"_blank\" rel=\"noopener\">Click Here<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<div id=\"sec-end\" class=\"sec-head\" style=\"background: #f1f1f1;\">\n<h2 class=\"in-head-d\">THE END<\/h2>\n<p>Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to a quick tutorial on how to read zip files in PHP. So the old zip functions are deprecated in PHP 8, how do we read files using Zip Archive then? This has caused some confusion, but it is actually very straightforward. To read zip files in PHP, we can use statIndex() to get [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":22535,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","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":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","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":""}},"footnotes":""},"categories":[14],"tags":[],"class_list":["post-331","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/posts\/331"}],"collection":[{"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/comments?post=331"}],"version-history":[{"count":0,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/posts\/331\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/media\/22535"}],"wp:attachment":[{"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/media?parent=331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/categories?post=331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-boxx.com\/wp-json\/wp\/v2\/tags?post=331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}