Zip a folder on the server with php

zip icon

I have a spent a day today figuring how to zip a client’s web site folder on a Godaddy shared Linux hosting today (they made sure users don’t have SSH access on those) in order to transfer it to another server. Apparently, given the limitations of the account setup the best way was to write a php script by executing which i would zip the given folder contents, so that then i could copy just one file and unzip it on the target server.

And it can’t be easier than that - if you have a folder, which contains a number of other folders and files in them the script below lets you add to the zip file quickly and easily:



<?
// increase script timeout value
ini_set('max_execution_time', 300);
// create object
$zip = new ZipArchive();
// open archive
if ($zip->open('my-archive.zip', ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
// initialize an iterator
// pass it the directory to be processed
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("app/"));
// iterate over the directory
// add each file found to the archive
foreach ($iterator as $key=>$value) {
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}
// close and save archive
$zip->close();
echo "Archive created successfully.";
?>

Proudly taken from the zend.com.

Tags: , ,

Friday, January 16th, 2009 php

2 Comments to Zip a folder on the server with php

  1. i just cant get it to work!! and i need this to work …

    please help.

  2. khanyawar on October 19th, 2009
  3. what seems to be the problem?

  4. admin on November 1st, 2009

Leave a comment

You must be logged in to post a comment.