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 Hacks, Hosting, PHP

You might also like

Intermedia hosting outsources technical support to Russia It has come as a surprise, when instead of an Indian person a girl with a russian accent answered my...
Make your LEMP website faster by installing eAccelerator The other day i had a discussion with someone about how fast Java-powered website are, because the code...
Apache vs. Nginx – testing performance under heavy load It has come to my attention, when a client of mine recently experienced unexpected traffic spike, that...
Thoughts about cloud hosting for Grails applications A few years ago nobody probably heard about "cloud computing" term. Except maybe for engineers building...

14 Comments to Zip a folder on the server with php

  1. Great Job, I have made some modification (see below); do you think it will work:

    < ?php
    open('myZip.zip', ZIPARCHIVE::CREATE) !== TRUE)
    {
    	die ("Could not open archive");
    }
    // initialize an iterator
    // pass it the directory to be processed
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("$dir/"));
    // 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. click to download";
    ?>
    
  2. tarek71 on April 12th, 2010
  3. Nice! Have to test it though…

  4. Oleksiy Deverishchev on April 12th, 2010
  5. Nice Script..
    It’s really help me.

  6. Vasant on June 16th, 2010
  7. this is a good script. but i find one problem in this script it does not include int empty folders in the archive. but i need those empty folders also. anyone can help me.

  8. mazia on June 17th, 2010
  9. It’s really very nice script.It helped me a lot.
    Thanks.

  10. Nisha on February 6th, 2011
  11. What a great script.
    Thanks.

  12. Raymond Chester on February 21st, 2011
  13. Thanks, great job.

  14. Chen on June 17th, 2011
  15. Hi

    Great script!

    Is there a way of using this to Zip the contents of the DIR without the DIR inside the zip, so that all files are at the root of the Zip and not in a DIR??

    Thanks in advance

  16. DC on July 21st, 2011
  17. I have a problem with any of the samples with Zipping that I find in the internet. When I look at the zipped file after the process, I always get the absolute folders create within the zip file.
    For example: C/Workspace/temporaray/ThemeFile/ThemeFile/…

    Should I just be getting folders starting from ThemeFile?

    Can anyone help me how to fix this??

  18. Johan Magos on August 2nd, 2011
  19. Really great script, but i added something:

    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($_SERVER["DOCUMENT_ROOT"].DIRECTORY_SEPARATOR.”application”));
    foreach ($iterator as $key => $value) {
    $zip->addFile(realpath($key), str_replace($_SERVER["DOCUMENT_ROOT"].DIRECTORY_SEPARATOR, ”, $key));
    }

    it is useful if using absolute paths.

  20. frasiek on August 29th, 2011
  21. It’s really very nice script.It helped me a lot.
    Thanks.

  22. sam on October 4th, 2011
  23. thanks webandblog code good

  24. Diễn đàn rao vặt on November 3rd, 2011
  25. Thanks a lot! worked perfectly

  26. NH on November 23rd, 2011
  27. Thanks and Great Script again Thanks alot

  28. Abdul Basit on November 28th, 2011

Leave a comment

About


Oleksiy Deverishchev is a freelance web developer living in New York City. In this blog he shares his experience with various gadgets and web technologies.

He is available for the following gigs:
  • Microsoft Access projects
  • website design and re-design services
  • php/MySQL developement
  • Java and Grails development
  • virtual private server (VPS) migration, setup and tune-up

For more information and hiring availability please send an email to alex@webandblog.com

Advertisment

Read more

Recent works / current clients

  • BellatAuto Inc - New York / New Jersey used car auto dealer: design and coding.
  • Colette Maison Lumiere - multimedia artist: design and coding for the CMS.
  • Complete Body & Spa - New York City personal training and gym management company: Wordpress template coding.
  • DaleStyle Blog Dale Sudakoff’s fashion blog: Wordpress template coding.
  • DoctorKalitenko.COM - Sergey Kalitenko : antiaging hollistic doctor performing bioidentical replacement therapy. CMS coding and design.
  • Great Jones Spa - premiere New York City Day Spa: coded and designed website and storefront

I am a Freelancers Union Member

Freelancers Union Member Badge