Create Zip Archive Using PHP is very simple just like zip files and folders on your desktop. PHP Zip Class has all the inbuilt functionality you need to zip files and that is online on the web that’s the great news right !  With the help og PHP Zip class and other available resources I developed a simple code so that you can convert selected files in the zip. The function accept array of files of any extension like docs file, PDFs, images etc.

This functionality is very good for web-based file manager where you can choose and download it in compressed format.

create zip using php

Download source script and view demo in action:

Download Source                           Demo

The PHP: Create Zip Archives Using PHP


< ?php
	$error = "";		//declare error holder
		$post = $_POST;		
		$file_folder = "files/";	// define folder to load files
		if(extension_loaded('zip')){	// Checking ZIP extension is available
			if(isset($post['files']) and count($post['files']) > 0){	// Checking files are selected here
				$zip = new ZipArchive();			// Load zip library	
				$zip_name = time().".zip";			// Zip name here
				if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){		// Opening zip file to load files
					$error .=  "* Sorry ZIP creation failed at this time
"; } foreach($post['files'] as $file){ $zip->addFile($file_folder.$file); // Adding files into zip } $zip->close(); if(file_exists($zip_name)){ // push to download the zip header('Content-type: application/zip'); header('Content-Disposition: attachment; filename="'.$zip_name.'"'); readfile($zip_name); // remove zip file is exists in temp path unlink($zip_name); } }else $error .= "* Please select file to zip
"; }else $error .= "* You dont have ZIP extension
"; } ?>


Feel free to post your comments and queries here.