How to create a zip file in ZipArchive with php

In this example, you will learn ‘how to add files to zip archive  with  PHP programming language. To  create a zip file, we need to use ZipArchive class. Use the conditional statement to add a file in the ZipArcive class. If the file exists, the create() function will generate a new zip file, in case of failure, it will print failed in creating zip file.

<?php
/*
* Script By    : Tutorial Publications
* Site URL    : http://tutpub.com
*/

$newzip = new ZipArchive;
$create = $newzip->open(‘two_file.zip’, ZipArchive::CREATE);//MENTION THE ZIP FILE NAME TO BE CREATED

if ($create=== TRUE) {
$newzip->addFile(‘file-1.txt’);//MENTION THE FIRST FILE TO ADD IN ZIP ARCHIVE
$newzip->addFile(‘file-2.txt’);//MENTION THE SECOND FILE TO ADD IN ZIP ARCHIVE
$newzip->close();
echo “Zip file successfully created”;
} else {
echo “Failed in creating zip file”;
}
?>

Feel free to inform me as comment, if You have any confusion with the above code.  Enjoy!!!

Author Info

Shah Alom

Hi, This is Mohammad Shah Alom, My passion is Programming & Web Development. I am Founder of Micro Solutions Bangladesh. My Facebook profile shahalom1983 & Twitter Profile shahalom_83