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 8012511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:21:09+00:00 2026-06-04T19:21:09+00:00

I need a simple function that writes an array of files to one zip

  • 0

I need a simple function that writes an array of files to one zip file. I found some code from an online tutorial and modified it slightly, but I can’t seem to get it to work. It creates the zip file, but when I try and extract it I get an error:

Windows cannot complete the extraction.
The Compressed (zipped) Folder '...' is invalid.

Here’s the code I’m working with:

public function create_zip($files = array(),$destination = '',$overwrite = false) {
      //if the zip file already exists and overwrite is false, return false
      if(file_exists($destination) && !$overwrite) { return 'file exists'; }

      $valid_files = array();
      if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
          //make sure the file exists
          if(file_exists($file)) {
            $valid_files[] = $file;
          }
        }
      }
      Zend_Debug::dump($valid_files);

      if(count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if($zip->open($destination, ZIPARCHIVE::CREATE) !== true) {
          return 'could not open zip: '.$destination;
        }
        //add the files
        foreach($valid_files as $file) {
          $zip->addFile($file);
        }
        //debug
        Zend_Debug::dump($zip->numFiles);
        Zend_Debug::dump($zip->status);

        $zip->close();

        //check to make sure the file exists
        return file_exists($destination);
      } else  {
        return 'no valid failes'. count($valid_files);
      }
}

The debug statements are printing out the following:

 For $valid_files - array of one file name (full path to file)
 For $zip->numFiles - 1
 For $zip->status - 0
 The function returns true.

Any ideas on what I’m doing wrong?

  • 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-06-04T19:21:10+00:00Added an answer on June 4, 2026 at 7:21 pm

    Just needed to add slashes before the file names.

    Found the answer here: using zipArchive addFile() will not add image to zip

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

Sidebar

Related Questions

I need a simple proxy PHP function/script that can download a file from a
So all I need is a simple function that sets it up (eating incoming
How to change HTML background with JavaScript Function? I need some simple function to
I have HTML + CSS text. I need some lib with simple function like
I need to write a simple function that will delete all entries in the
I need to write a simple function that when the person enter number of
How could one approximate Inverse Incomplete gamma function Г(s,x) by some simple analytical function
I've got some sample code that I'd like to refactor as I need it
I am trying to write a simple function to load an array from XML
I need 2 simple JavaScript functions that read and change the color of a

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.