Create Zip & Add file to it

Create Zip & Add file to it


Posted in : PHP Posted on : January 22, 2011 at 6:56 PM Comments : [ 0 ]

This section contains the detail about the Creating Zip & Add file to it.

Create Zip & Add file to it

As you know Zip is used to compress the content. PHP also provide functionality to compress content in zip.

To use the functions of zip, you need to enable php_zip.dll inside php.ini.

Example

In this section, we will first create object of the class ZipArchive. Using this object, you can create a zip file using open() function. After this we will add a file in this zip using addFile() function.

Given below the code :

<?php
$zip = new ZipArchive;
$file='ZippedContent/AddFileZip.txt';
$res = $zip->open('AddFileToZip.zip', ZipArchive::CREATE);
if ($res=== TRUE) {
//Add the same file with different name say, 'AddFileToZip.txt'.
$zip->addFile($file,'AddFileToZip.txt');
$zip->close();
echo 'Zip file successfully created';
} else {
echo 'Failed in creating zip file';
}
?>

Output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics