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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 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