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

<?php

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

if(extension_loaded('zip'))
{
    // 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";
    }

    $i=1;
    
    foreach($arr as $file)
    {
        $zip->addFile($file,$i."-".basename($file)); // Adding files into zip
        $i++;
    }
    
    $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 .= "* You dont have ZIP extension";
}

?>

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

Advertisements

ZipArchive is not working in Extbase + typo3


 
When we create a new zips “$zip = new ZipArchive();”

However, we get always the following error message:

Fatal error: Class ‘TYPO3 \ .. \ Domain \ Repository \ ZipArchive’ not found in … / Classes / Domain / Repository / XXXRepository.php on line 143.

Zip works nicely on the server. A simple PHP script which zips a file running properly in server.

In Extbase we have to add backward slash [ \ ] infront of class name.

Following script should do the trick.

$zip = new \ZipArchive();

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