how to zip files in PHP

Posted on 2007-04-08
Last Modified: 2013-12-13
Hi! I'm trying to achieve multiple files and get one .zip file. here is the code

$zip = new ZipArchive();
$filename = "";

if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");


echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";

However, the program fails to zip the files and the output is
numfiles: 0 status:11

Does anybody know why it doesn' work. Thank you
Question by:izloch2
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 24

Accepted Solution

slyong earned 250 total points
ID: 18874549

Could you echo the status after the open?  As such:

$zip = new ZipArchive();
$filename = "";

if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");

echo "status:" . $zip->status . "\n";

If I am correct (I am not sure) status 11 is that the zip file exists.

Assisted Solution

ilava earned 250 total points
ID: 18875497

Try this code it will work fine..

i think the problem in your code is the given path and the files. i.e., the files may not be in that path.

$zip = new ZipArchive();
$filename = "d:\\";

if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");

$zip->addFile($thisdir . "D:\\test1.txt","d:\\test2.txt");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";

it will gives the output as numfiles: 2 status:0

total archived files are 2.

also check before that php_zip.dll is enabled in your php.ini file


Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Wordpress Query 5 45
xml with php question 5 37
Why is my $_POST not going to results page 10 41
Compiling PHP with Curl plus protocols 8 25
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question