Social Networks

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<br />";
				foreach($post['files'] as $file){				
					$zip->addFile($file_folder.$file);			// Adding files into zip
					// push to download the zip
					header('Content-type: application/zip');
					header('Content-Disposition: attachment; filename="'.$zip_name.'"');
					// remove zip file is exists in temp path
				$error .= "* Please select file to zip <br />";
			$error .= "* You dont have ZIP extension<br />";


Feel free to post your comments and queries here.

  1. i have one error in that code that is * You dont have ZIP extension what they mean to say ???? can you please resolve my error???

  2. CoursesWeb (@CoursesWeb) says:

    // Function to Extract ZIP Archive. Returns an Array with the path and name of each file in archive, or error message
    // receives 2 arguments: the path-name of the Zip file to open, the directory on server where the ZIP will be extracted
    function extractZip($zip_file, $dir_extract) {
    // PHP-MySQL Course –
    $re_arr = array(); // will store and return the name of the files in archive

    // create ogject of ZipArchive class, and open $zip_file
    $zip = new ZipArchive();
    $res = $zip->open($zip_file);

    // if the $zip_file can be opened
    if($res === TRUE) {
    // traverse the index number of the files in archive, store in array the name of the files in archive
    for($i = 0; $i numFiles; $i++) {
    $re_arr[] = $zip->getNameIndex($i);

    // extract the files

    return $re_arr;
    else echo “Failed to open $zip_file , code: $res”;

    • PHP 5+: Zip functions and the Zip library is not enabled by default and must be downloaded from the links above. Use the –with-zip=DIR configure option to include Zip support.

Leave a Reply

Your email address will not be published. Required fields are marked *

Allowed Tags:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>