getButterfly Logo getButterfly


Let’s suppose you want to create a small PHP function to archive various folders on your server. I had to code a plugin to generate archives from various folders for a site with no FTP access (don’t ask).

Excluding the WordPress specific code, here’s the HTML form:

<form method="post" action="" name="zipMe">
    <p><input type="text" name="folderPath" id="folderPath" placeholder="Path to your directory..."> <label for="folderPath">Path to your directory</label></p>
    <p>input type="submit" name="zipMeNow" value="Zip and download"></p>

Here’s the PHP code:

if(isset($_POST['zipMeNow'])) {
    $folderPath = $_POST['folderPath']; // sanitize this value

    ini_set('max_execution_time', 3600); // increase PHP execution time for large files

    // get path for the folder // I have zipped the /themes/ folder as an example
    $rootPath = '/home/xxxxxxxx/public_html/wp-content/themes/';

    $zip = new ZipArchive();
    $zip->open('', ZipArchive::CREATE | ZipArchive::OVERWRITE); // change filename or make it dynamic

    $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);

    foreach($files as $name => $file) {
        if(!$file->isDir()) { // skip directories (they will be added automatically)
            // get real and relative path for current file
            $filePath = $file->getRealPath();
            //$relativePath = substr($filePath, strlen($rootPath) + 1); // use this if you get a corrupt archive
            $relativePath = substr($filePath, strlen($rootPath));

            // add current file to archive
            $zip->addFile($filePath, $relativePath);

    // Zip archive will be created only after closing object

    // send headers to force download the file // or send it to Dropbox or Google Drive
    header("Content-type: application/zip");
    header("Content-Disposition: attachment;");
    //header("Content-length: " . filesize('')); // not necessary
    header("Pragma: no-cache");
    header("Expires: 0");

You don’t need to worry as the ZipArchive is a native PHP class. I am checking for its existence inside WordPress using this code:

if(class_exists('ZipArchive')) {
    echo 'ZipArchive class exists';

What you can improve:

1. Ask user for file name
2. Append text to the end of the file name (e.g.
3. Generate a custom folder list or read the actual directory structure on your server

Based on my previous client work, I’ll probably release this as a plugin.

Image source

Subscribe to getButterfly Blog

Once a week or so we send an email with our best content. We never bug you, we just send you our latest piece of content.

If you found any value in this post, agree, disagree, or have anything to add - please do. I use comments as my #1 signal for what to write about. Read our comment policy before commenting! Comments such as "Thank you!", "Awesome!", "You're the man!" are either marked as spam or stripped from URL.

Leave a Reply

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