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."; ?>
29 Comments
Great Job, I have made some modification (see below); do you think it will work:
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
Thanks, it was really very helpful 🙂
I was searching hopeless. now I’m speechless. thanks man.
Thanks a lot, this was the only script that wasn’t bloated and hard to follow I found.
I create zip on this method ,it create zip but when i install wordpress then it generate error PCLZIP_ERR_BAD_FORMAT (-10) : Invalid archive structure, plz help me how to solve this error
Legendary!!!!!
it is generating corrupted file… in the zip folder
Just nice
thanks.this helps me more than you think 😀
Very, Very, Nice! How can I modify this to check if my zip file is more than x days old? I would like to use it with a cron task to automate a backup process.
Hi
looked for such a script, nothing found that works, but yours is simple and works well.
Thanks & Congrats
Lionel
Is very useful thank you for posting this
Thanks
Thanks you dude. I recently wrote an article on how to unzip a previously zipped folder on your web server. So I allowed myself to use your article on my blog as a continuation of that article.
I gave credit to you though: http://samuel.blue-colibri.com/en/how-to-zip-a-folder-on-your-web-server-with-php/
Great and easy tutorial. I am using this snippet for create zip file.
function Zip($source, $destination)
{
if (!extension_loaded(‘zip’) || !file_exists($source)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace(‘\\’, ‘/’, realpath($source));
if (is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file)
{
$file = str_replace(‘\\’, ‘/’, realpath($file));
if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . ‘/’, ”, $file . ‘/’));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . ‘/’, ”, $file), file_get_contents($file));
}
}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
Thanks for sharing with us.
how to send zip file on another server,? can we zip multiple site in this script. i am also work on this whole day but we didn’t find any proper solution .please help us.