Solved

how to zip files in PHP

Posted on 2007-04-08
4
1,421 Views
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 = "test.zip";

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

$zip->addFile("file1.jpg");
$zip->addFile("file2.jpg");

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

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
0
Comment
Question by:izloch2
4 Comments
 
LVL 24

Accepted Solution

by:
slyong earned 250 total points
ID: 18874549
Hi,

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

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

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.
0
 
LVL 3

Assisted Solution

by:ilava
ilava earned 250 total points
ID: 18875497
Hi

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.
<?php

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

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";
$zip->close();
?>

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

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now