Florian Müller Florian Müller - 6 months ago 27
PHP Question

No error when creating zip, but it doesn't get created

I wrote this code to create a ZIP file and to save it. But somehow it just doesn't show any error, but it doesn't create a ZIP file either. Here's the code:

$zip = new ZipArchive;
$time = microtime(true);
$res = $zip->open("maps/zips/test_" . $time . ".zip", ZipArchive::CREATE);
if ($res === TRUE) {
echo "RESULT TRUE...";
$zip->addFile("maps/filename.ogz","filename.ogz"); //Sauerbraten map format
$zip->addFromString('how_to_install.txt', 'Some Explanation...');
$zip_created = true;
echo "FILE ADDED!";

What am I doing wrong, and how can I fix it?

Answer Source

Probably apache or php has not got permissions to create zip archives in that directory. From one of the comments on ZipArchice::open:

If the directory you are writing or saving into does not have the correct permissions set, you won't get any error messages and it will look like everything worked fine... except it won't have changed!

Instead make sure you collect the return value of ZipArchive::close(). If it is false... it didn't work.

Add an else clause to your if statement and dump $res to see the results:

if($res === TRUE) {
} else {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download