How to create a zip file in php?

In one of my previous post, We  have already seen How we can extract Zip Files using PHP. In this tutorial, we are going to create a Zip file with PHP. To perform this task, we are going to use a built-in extension in PHP known as ZipArchive classs.

This is a very basic PHP function which accepts two parameters array of files to be zipped and name of zip file to be created.

//Creating a Zip File Using PHP
function genZip($files = array(),$zipName){
	$zip = new ZipArchive();
	$zip->open($zipName.'.zip', ZipArchive::CREATE);
	foreach($files as $file){


//Usage of genZip function
$files = array(
$zipName = 'myfiles';


Leave a Comment.