Florian Müller
Florian Müller

Reputation: 7795

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->close();
    $zip_created = true;
    echo "FILE ADDED!";
}

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

Upvotes: 27

Views: 45220

Answers (6)

Vinaykumar Patel
Vinaykumar Patel

Reputation: 884

One of the reasons for zip file is not created is due to missing check if you are adding file and not a directory.

if (!$file->isDir())

I found the solution here.

Upvotes: 1

Bob
Bob

Reputation: 2510

I had an exactly same issue, even when with full writing/reading permissions.

Solved by creating the ".zip" file manually before passing it to ZipArchive:

$zip = new ZipArchive;
$time = microtime(true);
$path = "maps/zips/test_" . $time . ".zip"

touch($path);  //<--- this line creates the file

$res = $zip->open($path, 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->close();
    $zip_created = true;
    echo "FILE ADDED!";
}

Upvotes: 11

Pascal Messana
Pascal Messana

Reputation: 277

Check out that each of your file exists before calling $zip->addFile otherwise the zip won't be generated and no error message will be displayed.

if(file_exists($fichier->url))
{
    if($zip->addFile($fichier->url,$fichier->nom))
    {
        $erreur_ouverture = false;
    }
    else
    {
        $erreur_ouverture = true;
        echo 'Open error : '.$fichier->url;
    }
}
else
{
    echo 'File '.$fichier->url.' not found';
}

Upvotes: 9

user1720209
user1720209

Reputation: 121

There are 2 cases when zip doesn't generate the error.

  1. Make sure every file you are adding to the zip is valid. Even if one file is not available when
    zip->close is called then the archive will fail and your zip file won't be created.
  2. If your folder doesn't have write permissions zip will not report the error. It will finish but nothing will be created.

Upvotes: 12

zod
zod

Reputation: 12437

break it into steps.

if ($res === TRUE) {

  check if file_exist

check if addFile give any error
}

if($zip->close())
{
 $zip_created = true; 
    echo "FILE ADDED!"
}

Check the phpinfo for zip is enabled or not :)

Upvotes: 1

alexn
alexn

Reputation: 59022

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 {
    var_dump($res);
}

Upvotes: 16

Related Questions