How to compress directory into Zip file in php

Some Hosting Server has no facility to compress the directory or files into zip file so that if our data is too larger then we can’t download the directory manually using ftp because it is taking too much time to download it.

The following code is the way by using you can compress your directory.


ini_set('max_execution_time', 8000);

// Create an object
$zip = new ZipArchive();

// Create the Zip or Archive file
if ($zip->open('', ZIPARCHIVE::CREATE) !== TRUE)
die ("Could not open archive");

// Pass the Directory which you want to compress
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("C:/Program Files/EasyPHP-"));

// Add each file of the directory into the Archive file
foreach ($iterator as $key=>$value)
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");

// Close and Save the Archive data
echo "Archive created successfully.";

Find the Following code from the above given code for compressing the directory into Zip file.

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("C:/Program Files/EasyPHP-"));

And write your directory name with actual path where your directory exist. That’s it.

I hope this will help you.

Speak Your Mind

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: