How to Create and Download a Zip File From Folder in PHP?
Teams applying “How to Create and Download a Zip File From Folder in PHP?” in a production project can use the official walkthrough to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the PHP project and test it against the project’s actual database and runtime.
Hi Dev,
Now, let's see tutorial of how to create and download a zip file from folder in php. it's simple example of create & download zip file using php. We will use create zip format and download whole folder or file using php. I’m going to show you about create zip and download file using php.
Start the tutorial for creating a simple zip file from folder in PHP projects or web applications:
Step 1: Create a Index.php File
First of all create a index.php file and update the following code into your index.php file:
<?php
/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$zip->addFile($file,$file);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
$files_to_zip = array(
'D:/xampp/htdocs/phptest.php',
'D:/xampp/htdocs/index.php',
);
//if success than true. if false, zip creation failed
$result = create_zip($files_to_zip,'my-archive.zip');
?>
We have created a function in PHP called create_zip (). Which compresses and downloads files or folders into zip.
see that the function named create_zip () is called in the last of PHP script. This function compresses files and folders in the zip. And If you want to change the name of the zip file that will be created and downloaded, you can also change its name easily.
NOTE:- Where you create_zip ($ files_to_zip, ‘my-archive.zip’); Under “my-archive.zip” it has been done, instead you can keep any name you want to keep.
You have to go to the browser. And hit the following URL:
http://localhost/index.php
The zip file we named “my-archive.zip” was converted to such zip after downloading. You can see in the image below.
I hope it could help you...
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example