PHP Create Zip File From Directory

Whenever you are working in PHP Projects or web applications. So many times you need to create a zip file in your PHP projects. This tutorial will show you the easy and simple way how to create a zip file in PHP and can also download the zip file.

php create zip file from directory. Here you will learn how to create zip file from directory in php, or php zip folder and subfolders, php create zip file and download, php zip addfile, php exec zip folder, php zip csv file,, zip archive, php zip archive extension, etc.

And instead of php file, you can compress file like excel file, txt file, csv, etc. from given php script in zip.

How to create and download a Zip file with PHP

Let’s start tutorial to creating a simple zip file in PHP projects or web applications:

Create a Index.php File

First of all create a index.php file and update the following code into your index.php file:

open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
			return false;
		//add the files
		foreach($valid_files as $file) {
		//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
		//close the zip -- done!
		//check to make sure the file exists
		return file_exists($destination);
		return false;

$files_to_zip = array(

//if success than true. if false, zip creation failed

$result = create_zip($files_to_zip,'');


We have created a function in PHP called create_zip (). Which compresses and downloads files or folders into zip.

You will 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, ‘’); Under “” it has been done, instead you can keep any name you want to keep.

Now you have to go to the browser. And hit the following URL


The zip file we named “” was converted to such zip after downloading. You can see in the image below.

php create zip file and download

Images mentioned above related to PHP are either copyright property of respective image owners.

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is the owner of RabinsXP who is constantly working for increasing the Internet of Things (IoT) in Nepal. He also builds android apps and crafts beautiful websites. He is also working with various social services. The main aim of Lamichhane is to digitally empower the citizens of Nepal and make the world spiritually sound better both in terms of technology and personal development. Rabins is also the first initiator of Digital Nepal.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button