Compressing Multiple Files as Zip and Download it using PHP
In this tutorial, you will learn how to Compress Multiple Files as Zip and Download it using PHP Language. The tutorial aims to provide IT/CS Students and new programmers with a reference on using PHP ZipArchive(). Here, I will be providing a simple web application's snippets and source code zip file which demonstrates our main objective in this tutorial.
What is Zip File?
One or more compressed files or directories may be present in a ZIP file. It is an archive format for lossless data compression and one of the most widely used compression formats in the world.
What is PHP ZipArchive?
The ZipArchive is a class in PHP that contains multiple methods used to perform several zipping operations. It is used for adding new files/directories, extracting, opening, and closing ziparchive content.
PHP ZipArchive Syntax?
The snippet below is an example of how to use the PHP ZipArchive.
- <?php
- $zip = new ZipArchive();
- /**
- * ZipArchive Flags
- * - ZipArchive::OVERWRITE
- * - ZipArchive::CREATE
- * - ZipArchive::RDONLY
- * - ZipArchive::CHECKONS
- */
- $zip->open("zip_filename.zip", [ZipArchive Flags])
- $zip->close();
How to Compress Multiple Files as Zip and Download it in PHP?
Here are the following snippets or source code of a simple web application written in PHP and HTML that demonstrates file compressions using ZipArchive and Download the created Zip file.
First, create a new directory named uploads in your source code directory. Add some sample files inside the directory to use on this sample web application.
Interface
Next, create a new PHP file and save it as index.html. The file contains the following script. The snippet below is a combined script of HTML and PHP for displaying the page interface, form, and list of files. Note that the snippet below loads the Bootstrap Framework using CDN which needs you to be connected to the internet to display the page design properly.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
- <link rel="stylesheet" href="style.css">
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
- </head>
- <body style="background:#e1ecf7">
- <nav class="navbar navbar-expand-lg navbar-dark" style="background:#1597BB">
- <div class="container">
- <div>
- </div>
- </div>
- </nav>
- <div class="container-fluid px-5 my-3 pt-5">
- <div class="col-lg-6 col-md-8 col-sm-12 mx-auto">
- <hr>
- <div class="card rounded-0">
- <div class="card-header rounded-0">
- </div>
- <div class="card-body rounded-0">
- <div class="container-fluid">
- <form action="zip_and_download.php" id="download-form" method="POST">
- <div class="list-group">
- <?php
- $scandir = scandir("uploads/");
- $i = 1;
- foreach($scandir as $file):
- if(in_array($file, ['.', '..']))
- continue;
- ?>
- <div class="list-group-item">
- <div class="d-flex w-100">
- <div class="col-auto">
- <div class="form-check">
- <input class="form-check-input" type="checkbox" name="fileName[]" value="<?= $file ?>" id="file_<?= $i ?>">
- </div>
- </div>
- </div>
- </div>
- <?php
- $i++;
- endforeach;
- ?>
- </div>
- </form>
- </div>
- </div>
- <div class="card-footer py-2 text-center">
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
PHP API
Then, create a new PHP file and save it as zip_and_download.php. The file contains the following snippet. The snippet is a PHP script for creating a zip file containing the compressed files selected from the form on the main page and automatically or forcing the page to download the created zip file.
- <?php
- if($_SERVER['REQUEST_METHOD'] == "POST"){
- //Zip filename
- $zip_fname = "compressed_zip_file.zip";
- //Removing the file if already exist
- // Load the ZipArchive Class
- $zip = new ZipArchive();
- //Opening/Create a new zip archive
- if ($zip->open($zip_fname, ZIPARCHIVE::CREATE )!==TRUE) {
- }
- // Adding the selected files to the archive
- foreach($_POST['fileName'] as $file)
- {
- $zip->addFile("uploads/".$file, $file);
- }
- //closing the zip
- $zip->close();
- //Force to download the created zip file
- }
Snapshots
Here are some of the application's snapshots.
There you go! You can now test the application on your end and see if it achieves our main objective for the tutorial which is to compress multiple files into a zip file and download it. I have also provided the complete source code zip file that I created for this tutorial. You can download it by clicking the Download Button located below this article.
That's the end of this tutorial. I hope this Compressing Multiple Files as Zip and Download it using PHP Tutorial helps you with what you are looking for and that you'll find this useful for your current and future PHP Projects/.
Explore more on this website for more Tutorials and Free Source Codes.
Happy Coding:)
Add new comment
- 1403 views