How to unzip or extract zip file using PHP

How to unzip or extract zip file using PHP

Here is the simple code which will help you to unzip or extract zip file using php. In this tutorial we are using ZipArchive class of php to do so. ZipArchive is a built in class of PHP. ZipArchive was introduced in PHP version 5.2. So, your PHP version should be 5.2 or greater. So friends, in this tutorial you will see how to unzip or extract zip file using PHP. Please visit the following link which will show you how to create a zip file using php.

 

How to unzip or extract zip file using php

Now use the below code to unzip or extract zip file.

  1. <?php
  2. //Check whether zip extension is enabled or not
  3. if(extension_loaded('zip')) {
  4. $zip = new ZipArchive();
  5.  
  6. //Path of the source zip file to be extracted
  7. $source = "test.zip";
  8. if ($zip->open($source) === TRUE) {
  9. //Destination of extracted files and folders to be stored
  10. $destination = "extracted/";
  11. $zip->extractTo($destination);
  12. $zip->close();
  13. } else {
  14. echo "Failed to open the zip file!";
  15. }
  16. }
  17. ?>
  1. <?php
  2. //First check whether zip extension is enabled or not
  3. if(extension_loaded('zip')) {
  4. $zip = new ZipArchive();
  5. ?>

Check whether the zip extension is enabled or not. If yes, then create an object of the ZipArchive class. This object will help you to call various default functions of the ZipArchive class.

  1. <?php
  2. if($zip->open($source) === TRUE) {
  3. ----------------------
  4. ----------------------
  5. } else {
  6. echo "Failed to open the zip file!";
  7. }
  8. ?>

If the source zip file is not found it will simply print a message that “Failed to open the zip file!”.

  1. <?php
  2. //Destination of extracted files and folders to be stored
  3. $destination = "extracted/";
  4. $zip->extractTo($destination);
  5. $zip->close();
  6. ?>

$zip->extractTo() function extracts zip file and stores all the files and folders of it in the predefined folder. $zip->close() simply close and free the resource.

Here is the complete code of the article “How to unzip or extract zip file using php” —

  1. <?php
  2. if(isset($_POST['extractZip'])) {
  3.  
  4. //Check whether zip extension is enabled or not
  5. if(extension_loaded('zip')) {
  6. $zip = new ZipArchive();
  7.  
  8. //Path of the source zip file to be extracted
  9. $source = "test.zip";
  10. if ($zip->open($source) === TRUE) {
  11. //Destination of extracted files and folders to be stored
  12. $destination = "extracted/";
  13. $zip->extractTo($destination);
  14. $zip->close();
  15. } else {
  16. echo "Failed to open the zip file!";
  17. }
  18. } else {
  19. echo "Zip extension is not enabled!";
  20. }
  21. }
  22. ?>
  23.  
  24. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  25. <html xmlns="http://www.w3.org/1999/xhtml">
  26. <head>
  27. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  28. <title>How to extract or unzip a zip file using PHP || Mitrajit's Tech Blog</title>
  29. <style>
  30. span { clear:both; display:block; margin-bottom:30px; }
  31. span a { font-weight:bold; color:#0099FF; }
  32. table { margin-top:30px; }
  33. .msg-div { margin-top:20px; }
  34. ul { margin:0; padding:0; }
  35. ul li { list-style:none; margin:10px; }
  36. img { margin-right:10px; }
  37. h3 { border-bottom:2px solid #000; }
  38. </style>
  39. </head>
  40.  
  41. <body>
  42. <span>Read the full article -- <a href="http://www.mitrajit.com/2016/10/unzip-extract-zip-file-using-php/" target="_blank">How to unzip or extract zip file using PHP</a> in Mitrajit's Tech Blog</span>
  43.  
  44. <form action="" method="post">
  45. <input type="submit" name="extractZip" value="Extract Zip" />
  46. </form>
  47.  
  48. <?php
  49. if(isset($_POST['extractZip'])) {
  50. echo "<h3>Extracted files and folders</h3>";
  51. echo "<ul>";
  52. foreach(glob('extracted/*') as $filename)
  53. {
  54. echo "<li>";
  55. $filenames = explode("/", $filename);
  56. if(is_dir($filename)) echo "<img src='images/folder.gif' width='25' height='25' align='absmiddle'>".$filenames[1];
  57. else echo "<img src='images/file.gif' width='25' height='25' align='absmiddle'>".$filenames[1];
  58. echo "</li>";
  59. }
  60. echo "</ul>";
  61. }
  62. ?>
  63. </body>
  64. </html>

If you enjoy this article and think it is useful then please share this article with others.

Need a Website Or Web Application Or Any Help In Code , Contact Us: +91 8778409644 (Whatsapp) or Email: uma@f5craft.com | Visit: www.f5craft.in /.com, Note: Paid Service Only