Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6831783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:46:41+00:00 2026-05-26T22:46:41+00:00

The following function works great, but it only zips the 1 folder. I need

  • 0

The following function works great, but it only zips the 1 folder. I need to zip several specific folders in the root of a site but not all of the rest.

Source: How to [recursively] Zip a directory in PHP?

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();

Called with this:

Zip('/folder/to/compress/', './compressed.zip');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:46:41+00:00Added an answer on May 26, 2026 at 10:46 pm

    If I understand you correctly, you can simply change your code to expect $source to be either string or array. Then you can loop through that array and add all desired folders and their sub-folders

    Examples:

    function Zip($source, $destination)
    {
        if (is_string($source)) $source_arr = array($source); // convert it to array
    
        if (!extension_loaded('zip')) {
            return false;
        }
    
        $zip = new ZipArchive();
        if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
            return false;
        }
    
        foreach ($source_arr as $source)
        {
            if (!file_exists($source)) continue;
    $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();
    
    }
    

    Zip might be empty of all specified paths are invalid but you can take care of that too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code,the hover function works correctly, but when clicked, loses the clicked_no_event_box
I have the following code which works great:- $('.ajax a').click(function() { getpageajax(this.href); return false;
I have the following function which works very well within a $(document).ready(function(){ $('.threadWrapper >
How does the following grep function works (what does !/0o1Iil]/ do? ) @chars =
I used the following function I found online and it works perfectly. However, when
I have the following code that works on Firefox and Chrome $("#adicionarItem").click(function(){ $.ajax({ type:
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
I can't get the following function in VBScript to work. I am trying to
How come the following doesn't work? CREATE FUNCTION Test (@top integer) RETURNS TABLE AS
I have the following code to enable validation work: $(document).ready(function() { $(#eventForm).validate({ rules: {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.