Theme Night

Php Zip Functions

Home » Php » Php Zip Functions

PHP Zip Introduction

The Zip files functions allows you to read ZIP files.


The ZIP extension requires libzip.


Linux Systems

For these functions to work, you have to compile PHP with --enable-zip.

PHP 5.6: Use the --with-libzip=DIR configure option to use a system libzip installation. libzip version 0.11 is required, with 0.11.2 or later recommended.

PHP 7.3: Building against the bundled libzip is discouraged, but still possible by adding --without-libzip to the configuration.

Windows Systems

Before PHP 5.3: Users must enable "php_zip.dll" inside of "php.ini" for these functions to work.

From PHP 5.3: The ZIP extension is built-in.

Function Description
zip_close() Closes a ZIP file archive
zip_entry_close() Closes a ZIP directory entry
zip_entry_compressedsize() Returns the compressed file size of a ZIP directory entry
zip_entry_compressionmethod() Returns the compression method of a ZIP directory entry
zip_entry_filesize() Returns the actual file size of a ZIP directory entry
zip_entry_name() Returns the name of a ZIP directory entry
zip_entry_open() Opens a directory entry in a ZIP file for reading
zip_entry_read() Reads from an open directory entry in the ZIP file
zip_open() Opens a ZIP file archive
zip_read() Reads the next file in a open ZIP file archive

ziparchive php example

    $error = "";        //error holder
        $post = $_POST;     
        $file_folder = "files/";    // 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
                $zip = new ZipArchive();            // Load zip library 
                $zip_name = time().".zip";          // Zip name
                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/>";
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Download As Zip</title>
<center><h1>Create Zip</h1></center>
<form name="zips" method="post">
<?php if(!empty($error)) { ?>
<p style=" border:#C10000 1px solid; background-color:#FFA8A8; color:#B00000;padding:8px; width:588px; margin:0 auto 10px;"><?php echo $error; ?></p>
<?php } ?>
<table width="600" border="1" align="center" cellpadding="10" cellspacing="0" style="border-collapse:collapse; border:#ccc 1px solid;">
    <td width="33" align="center">*</td>
    <td width="117" align="center">File Type</td>
    <td width="382">File Name</td>
    <td align="center"><input type="checkbox" name="files[]" value="a.jpg" /></td>
    <td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
    <td align="center"><input type="checkbox" name="files[]" value="b.jpg" /></td>
    <td align="center"><img src="files/image.png" title="Image" width="16" height="16" /></td>
    <td align="center"><input type="checkbox" name="files[]" value="c.docx" /></td>
    <td align="center"><img src="files/doc.png" title="Document" width="16" height="16" /></td>
    <td align="center"><input type="checkbox" name="files[]" value="d.pdf" /></td>
    <td align="center"><img src="files/pdf.png" title="pdf" width="16" height="16" /></td>
    <td colspan="3" align="center">
        <input type="submit" name="createpdf" style="border:0px; background-color:#800040; color:#FFF; padding:10px; cursor:pointer; font-weight:bold; border-radius:5px;" value="Download as ZIP" /> 
        <input type="reset" name="reset" style="border:0px; background-color:#D3D3D3; color:#000; font-weight:bold; padding:10px; cursor:pointer; border-radius:5px;" value="Reset" />


Php Related Post