Create and Download as Zip files using PHP tutorial!

phpzip

In this tutorial we are going to see how to create and download multiple files as ZIP using PHP.
Note this: ZIP extension should be installed in your PHP server for this Script to work correctly.

Here is the create_zip() function which you can just copy paste and include it in any of your project.

// create zip function 
function create_zip($files = array(), $folderpath){
		// Check if the ZIP extension is available or not
		if(!extension_loaded('zip')){	
			$zip = new ZipArchive();			//create an object of Zip Library
			$zip_name = "download_".time().".zip";			// Zip name

			//create a zip file (ZIPARCHIVE::CREATE)
			$zip->open($zip_name, ZIPARCHIVE::CREATE);

			foreach($files as $file){	
				$zip->addFile($folderpath."/".$file);	
			}
			$zip->close();

		//lets download the zip if it is created
		if(file_exists($zip_name)){
			header('Content-type: application/zip');
			header('Content-Disposition: attachment; filename="'.$zip_name.'"');
			readfile($zip_name);
			//delete the file after download
			unlink($zip_name);
		}		
	}else{
		echo "ZIP extension is not installed in your server!!";
	}
}

Usage is very simple, Just look at the below example:

$files = array('file1.doc', 'file2.jpg', 'file3.png', 'file4.pdf');
create_zip($files, 'myfiles'); //where myfiles is the folder name of the directory

If you see the above example, you can easily understand. All you have to do is pass the files in an array and give the correct folder path where the actual files are residing.

To make it more comfortable, Here is an amazing example (you can also see the demo or download the entire example below):

<?php

// create zip function 
function create_zip($files = array(), $folderpath){
		// Check if the ZIP extension is available or not
		if(extension_loaded('zip')){	
			$zip = new ZipArchive();			//create an object of Zip Library
			$zip_name = "download_".time().".zip";			// Zip name

			//create a zip file (ZIPARCHIVE::CREATE)
			$zip->open($zip_name, ZIPARCHIVE::CREATE);

			foreach($files as $file){	
				$zip->addFile($folderpath."/".$file);	
			}
			$zip->close();

		//lets download the zip if it is created
		if(file_exists($zip_name)){
			header('Content-type: application/zip');
			header('Content-Disposition: attachment; filename="'.$zip_name.'"');
			readfile($zip_name);
			//delete the file after download
			unlink($zip_name);
		}		
	}else{
		echo "ZIP extension is not installed in your server!!";
	}
}			

//usage example of the above create zip function

if(isset($_POST['files'])){
	$files = $_POST['files'];
	create_zip($files, 'myfiles');
}else{
	echo "<h3 style='color:red'>Please select atleast one file to download</h3>";
}

?>
<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <title>"Select All" Checkboxex using jQuery</title>
	<script language="javascript">
$(function(){

    // select all / deselect all
    $("#selectall").change(function () {
         $(".mycheckbox").prop('checked', $(this).prop('checked'));
    });

    // find if all the checkboxes are checked, if then select the top one too.
    $(".mycheckbox").change(function(){
        if($(".mycheckbox").length == $(".mycheckbox:checked").length)
        $("#selectall").attr("checked", "checked");
			else
        $("#selectall").removeAttr("checked");
    });
});
</script>
</head>
<body>
    <h2>Download Multiple files as ZIP using PHP - blog.theonlytutorials.com</h2>
<form action="" method="post">
<table style="border: 1px solid gray;">
<tr style="background:blue; color:white;">
    <th	><input type="checkbox" id="selectall"/></th>
    <th align="left">Files</th>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="mycheckbox" name="files[]" value="myfile1.docx"/></td>
	<td>myfile1.docx</td>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="mycheckbox" name="files[]" value="myfile2.pdf"/></td>
    <td>myfile2.pdf</td>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="mycheckbox" name="files[]" value="myfile3.txt"/></td>
    <td>myfile3.txt</td>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="mycheckbox" name="files[]" value="myfile4.jpg"/></td>
    <td>myfile4.jpg</td>
</tr>
<tr>
    <td align="center"><input type="checkbox" class="mycheckbox" name="files[]" value="myfile5.png"/></td>
    <td>myfile5.png</td>
</tr>
<tr>
	<td colspan="2" align="right"><input type="submit" value="Download" /></td>
</tr>
</table>
</form>
</body>
</html>

DemoDownload

2 thoughts on “Create and Download as Zip files using PHP tutorial!

  1. Great tutorial! Thanks for sharing.

    Is there a simple way for make your script display existing files in a folder versus specifying them?

    Thanks again.

Leave a Reply

Theme: Overlay by Kaira
Agurchand