[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to zip files in PHP

Posted on 2007-04-08
4
Medium Priority
?
1,454 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
2 Comments
 
LVL 24

Accepted Solution

by:
slyong earned 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month18 days, 17 hours left to enroll

834 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