How to create zip file using PHP

How to create zip file using PHP

One of the most popular and vastly used compression format is zip. Any number of files and folders can be stored within a zip file in a compressed size. “.zip” is the extension of the zip file. One of the built in classes of PHP is ZipArchive and you need this class for creating zip file. ZipArchive was introduced in PHP version 5.2 therefore your PHP version should be 5.2 or greater. So friends, in this tutorial you will see how to create zip file using PHP.

How to create zip file using php

 

How to create zip file using PHP

Let’s see the following example which shows you how to create zip file using php. I have created a simple php script which will create the zip file with dynamic name.

  1. <?php
  2. //First check whether zip extension is enabled or not
  3. if(extension_loaded('zip')) {
  4. $zip = new ZipArchive();
  5.  
  6. //Creating a dynamic name of the zip file
  7. $dynamic_zip_name = date('d-m-Y_H-i-s').".zip";
  8. if($zip->open($dynamic_zip_name, ZipArchive::CREATE) === TRUE) {
  9.  
  10. //Add some files to the zip file
  11. $zip->addFile('mitrajit.jpg');
  12.  
  13. //Add a existing image with a new name
  14. $zip->addFile('create-zip-file.png', 'sample.png');
  15.  
  16. //Add image in the image folder with new name
  17. $zip->addFile("featured.jpg", "files/main-featured.jpg");
  18.  
  19. //Add a new file sample.txt with the following contents specified in example folder
  20. $zip->addFromString('example/sample.txt', 'text to be added to the sample.txt file');
  21.  
  22. //Add an empty folder
  23. $zip->addEmptyDir("empty_folder");
  24.  
  25. //Close
  26. $zip->close();
  27.  
  28. if(file_exists($dynamic_zip_name)) {
  29. $file_info = finfo_open(FILEINFO_MIME_TYPE);
  30. $mime_type = finfo_file($file_info, $dynamic_zip_name);
  31. $name = basename($dynamic_zip_name);
  32. header('Content-type: '.$mime_type);
  33. header('Content-Disposition: attachment; filename="' . $name . '"');
  34. readfile($dynamic_zip_name);
  35. unlink($dynamic_zip_name);
  36. }
  37. }
  38. } else {
  39. echo "zip extension is not enabled!";
  40. }
  41. ?>
  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. Each and every time a unique dynamic name will create for the zip file. I have used date and time for dynamic name creation.

  1. <?php if($zip->open($dynamic_zip_name, ZipArchive::CREATE) === TRUE) { ?>

The above line indicates that a new zip file will create. If you want to overwrite an existing zip file then you should use write “OVERWRITE” in place of “CREATE” like below.

  1. <?php if($zip->open($dynamic_zip_name, ZipArchive::OVERWRITE) === TRUE) { ?>

 

  1. <?php
  2. //Add some files to the zip file
  3. $zip->addFile('mitrajit.jpg');
  4.  
  5. //Add a existing image with a new name
  6. $zip->addFile('create-zip-file.png', 'sample.png');
  7. ?>

You can add files in the zip as it is. Also you can add files in the zip with new names.

  1. <?php
  2. //Add image in the image folder with new name
  3. $zip->addFile("featured.jpg", "files/main-featured.jpg");
  4. ?>

You can add files to any folder  or sub folder.

  1. <?php
  2. // Add a new file sample.txt with the following contents specified in example folder
  3. $zip->addFromString('example/sample.txt', 'text to be added to the sample.txt file');
  4.  
  5. //Add an empty folder
  6. $zip->addEmptyDir("empty_folder");
  7. ?>

You can add sample text file with predefined text contents  in it. Also it is possible to add empty folders in it.

  1. <?php
  2. //Close the object
  3. $zip->close();
  4. ?>

When a file is set to be added to the archive, PHP will lock the file. The lock is only released when the ZipArchive object has been closed via close() function.

  1. <?php
  2. //Checking whether the created zip file is exists or not
  3. if(file_exists($dynamic_zip_name)) {
  4. $file_info = finfo_open(FILEINFO_MIME_TYPE);
  5. $mime_type = finfo_file($file_info, $dynamic_zip_name);
  6. $name = basename($dynamic_zip_name);
  7. //Push the created zip file to the browser for download
  8. header('Content-type: '.$mime_type);
  9. header('Content-Disposition: attachment; filename="' . $name . '"');
  10. readfile($dynamic_zip_name);
  11. //At last remove the file
  12. unlink($dynamic_zip_name);
  13. }
  14. ?>

At last you should push the zip file with proper mime type to the browser for download.

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

  1. <?php
  2. if(isset($_POST['createZip'])) {
  3.  
  4. //First check whether zip extension is enabled or not
  5. if(extension_loaded('zip')) {
  6. $zip = new ZipArchive();
  7.  
  8. //Creating a dynamic name of the zip file
  9. $dynamic_zip_name = date('d-m-Y_H-i-s').".zip";
  10. if($zip->open($dynamic_zip_name, ZipArchive::CREATE) === TRUE) {
  11. //echo "Not enabled!";
  12.  
  13. //Add some files to the zip file
  14. $zip->addFile('mitrajit.jpg');
  15.  
  16. //Add a existing image with a new name
  17. $zip->addFile('create-zip-file.png', 'sample.png');
  18.  
  19. //Add image in the image folder with new name
  20. $zip->addFile("featured.jpg", "files/main-featured.jpg");
  21.  
  22. //Add a new file sample.txt with the following contents specified in example folder
  23. $zip->addFromString('example/sample.txt', 'text to be added to the sample.txt file');
  24.  
  25. //Add an empty folder
  26. $zip->addEmptyDir("empty_folder");
  27.  
  28. //Close
  29. $zip->close();
  30.  
  31. //Checking whether the created zip file is exists or not
  32. if(file_exists($dynamic_zip_name)) {
  33. $file_info = finfo_open(FILEINFO_MIME_TYPE);
  34. $mime_type = finfo_file($file_info, $dynamic_zip_name);
  35. $name = basename($dynamic_zip_name);
  36. //Push the created zip file to the browser for download
  37. header('Content-type: '.$mime_type);
  38. header('Content-Disposition: attachment; filename="' . $name . '"');
  39. readfile($dynamic_zip_name);
  40. //At last remove the file
  41. unlink($dynamic_zip_name);
  42. }
  43. }
  44. } else {
  45. echo "zip extension is not enabled!";
  46. }
  47. }
  48. ?>
  49.  
  50. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  51. <html xmlns="http://www.w3.org/1999/xhtml">
  52. <head>
  53. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  54. <title>How to create zip file using PHP || Mitrajit's Tech Blog</title>
  55. <style>
  56. span { clear:both; display:block; margin-bottom:30px; }
  57. span a { font-weight:bold; color:#0099FF; }
  58. table { margin-top:30px; }
  59. .msg-div { margin-top:20px; }
  60. </style>
  61. </head>
  62.  
  63. <body>
  64. <span>Read the full article -- <a href="http://www.mitrajit.com/2016/10/create-zip-file-using-php/" target="_blank">How to create zip file using PHP</a> in Mitrajit's Tech Blog</span>
  65.  
  66. <form action="" method="post">
  67. <input type="submit" name="createZip" value="Create Zip" />
  68. </form>
  69. </body>
  70. </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