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?
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.
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.
“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.)
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); }
There are 2 cases when zip doesn't generate the error.
zip->close
is called then the archive will fail and your zip file won't be created. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With