26

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?

Mat
  • 202,337
  • 40
  • 393
  • 406
Florian Müller
  • 7,448
  • 25
  • 78
  • 120

6 Answers6

16

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);
}
alexn
  • 57,867
  • 14
  • 111
  • 145
  • 2
    Ran into this issue at some point. Checking with is_writable() beforehand also helps.. in PHP it really helps to do that before just about any write, just to make sure. – kander Jan 10 '11 at 20:43
  • @alexn I made sure I have write permissions to my directories, however the close() method gives me FALSE. How should I do? Other than permissions, is there any other possibilities? –  Nov 16 '16 at 15:12
12

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.
Renjith K N
  • 2,613
  • 2
  • 31
  • 53
user1720209
  • 121
  • 1
  • 2
  • 1
    Great! in my case, I need to delete two DB dumps after adding to ZIP, si I deleted them just after the "$zip->addFile(...", so when $zip->close() was called It failed!, Now I'm testing if $zip->close() returns true, if so, then I delete the DB dumps. – razor7 Jun 01 '16 at 16:48
  • Thanks, your option 1 fixed my issue! – TVA van Hesteren Mar 25 '17 at 15:18
10

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!";
}
Bob
  • 2,430
  • 22
  • 27
  • 1
    I saw this behaviour too but when using $zip->open($file, ZipArchive::OVERWRITE). Using a touch just felt wrong, found a comment at http://php.net/manual/en/ziparchive.open.php by eric at webdeveric dot com, which suggested $zip->open($file, ZipArchive::CREATE | ZipArchive::OVERWRITE), which works - for both creating or overwriting. – Anthony Jul 18 '16 at 11:05
9

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';
}
Pascal Messana
  • 277
  • 4
  • 12
1

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.

Vinaykumar Patel
  • 864
  • 11
  • 26
1

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 :)

zod
  • 12,092
  • 24
  • 70
  • 106