Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 624
Florian Müller Avatar asked Jan 10 '11 20:01

Florian Müller


People also ask

Why can't I create a zip file?

The various reasons why you get an access denied error are: a: You may not have ownership of a file or folder. b: You may not have the proper permissions. c: The file or folder may be Encrypted.

What to do if Zip file is not opening?

Right-click on the zip file and choose Open with... Select Windows Explorer. If Windows Explorer is not an option, select Choose default program... and select Windows Explorer, then click OK. Click Extract all files at the top of the window or in the sidebar.

Why can I not unzip files?

“folder is invalid” or “Decompression Failed” First, double-check to make sure the file has fully downloaded. If the file size has fully downloaded then the problem is probably that your default unzip cannot handle certain file names (i.e., too long, they have certain emojis, etc.)


2 Answers

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); } 
like image 71
alexn Avatar answered Sep 28 '22 13:09

alexn


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.
like image 35
user1720209 Avatar answered Sep 28 '22 13:09

user1720209