Zip a folder on the server with php

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.
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
Leave a comment
About
Advertisment
Read more
New York rooftops are great!:
What is great about big city? Its rooftops! Check out these photos i took while visiting a friend ...
Jewish Yellow Pages - New York style failed marketing: When it comes to signing up new clients jewish people can sometimes go far. Further, than you can im...- Psychological aspects of workplace productivity: Something triggered my attention today as I was reviewing a newsletter draft for one of my clients ...
Nokia N75 - de-branding and updating firmware:
Nokia N75 has had a huge success as a first descent USA 3G phone and is being used by many,...
Zip a folder on the server with php:
I have a spent a day today figuring how to zip a client's web site folder on a Godaddy shared Li...
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






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"; ?>Nice! Have to test it though…
Nice Script..
It’s really help me.
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.
It’s really very nice script.It helped me a lot.
Thanks.
What a great script.
Thanks.
Thanks, great job.
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
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??
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.
It’s really very nice script.It helped me a lot.
Thanks.
thanks webandblog code good
Thanks a lot! worked perfectly
Thanks and Great Script again Thanks alot