Zip a folder on the server with php

Hacks Hosting 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.";
?>

29 Comments

  • tarek71 says:

    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=&gt;$value) 
    {
    	$zip-&gt;addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
    }
    
    // close and save archive
    $zip-&gt;close();
    echo "Archive created successfully. <a href="myZip.zip" rel="nofollow">click to download</a>";
    ?>
    
  • Vasant says:

    Nice Script..
    It’s really help me.

  • mazia says:

    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.

  • Nisha says:

    It’s really very nice script.It helped me a lot.
    Thanks.

  • Raymond Chester says:

    What a great script.
    Thanks.

  • Chen says:

    Thanks, great job.

  • DC says:

    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

  • Johan Magos says:

    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??

  • frasiek says:

    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.

  • sam says:

    It’s really very nice script.It helped me a lot.
    Thanks.

  • NH says:

    Thanks a lot! worked perfectly

  • Abdul Basit says:

    Thanks and Great Script again Thanks alot

  • Nisha says:

    Thanks, it was really very helpful 🙂

  • iman says:

    I was searching hopeless. now I’m speechless. thanks man.

  • Jay-rad says:

    Thanks a lot, this was the only script that wasn’t bloated and hard to follow I found.

  • Pankaj says:

    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

  • eceers says:

    Legendary!!!!!

  • Supriya Singla says:

    it is generating corrupted file… in the zip folder

  • Santanu Jana says:

    Just nice

  • fatemeh says:

    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.

  • Lionel says:

    Hi
    looked for such a script, nothing found that works, but yours is simple and works well.
    Thanks & Congrats
    Lionel

  • Asaithambi says:

    Is very useful thank you for posting this

  • Naresh says:

    Thanks

  • Samuel Guebo says:

    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/

  • Sajjad says:

    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.

  • dheerendra says:

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.