Solved

how to zip files in PHP

Posted on 2007-04-08
4
1,430 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
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.

777 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