Generate ZIP file using php

Following post will helps you to create a ZIP file using PHP.

We can zip more than one file, Following script will help you to generate zip file and download zip file


$arr = array("jainish.png","jainish1.png");

    // Checking ZIP extension is available
    $zip = new ZipArchive(); // Load zip library
    $zip_name = "jainish-".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";

    foreach($arr as $file)
        $zip->addFile($file,$i."-".basename($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 .= "* You dont have ZIP extension";


If you have any queries, please do not hesitate to contact me at Jainish Senjaliya


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s