PHP’s ZipArchive Class Not Creating Zip File (And No Errors)
The ZipArchive Class provides a quick and easy way to create, or extract, Zip files using PHP.
I did face a problem recently though when trying to build a zip file, and it took me a while to figure out the answer. I would open a new Zip reference, add some files, and then close the zip immediately after. Oddly enough however there was no zip actually created and, even weirder, no errors informing me something had gone wrong with the build.
Let’s take a quick look at some code that builds a zip. Afterwards we’ll analyse what’s wrong with the code, why it won’t work, and how we can easily fix it:
- // Initiate a new instance of ZipArchive
- $zip = new ZipArchive();
- // Open a new zip file
- $res = $zip->open("myZip.zip", ZipArchive::CREATE);
- // Add a file to the zip file
- $zip->addFile("/path/to/file/myPhoto1.jpg", "newPhoto1.jpg");
- // Add a file to the zip file
- $zip->addFile("/path/to/file/myPhoto2.jpg44", "newPhoto2.jpg");
- // Add a file to the zip file
- $zip->addFile("/path/to/file/myPhoto3.jpg", "newPhoto3.jpg");
- // Close the zip file.
- // At this point it should be created
- $zip->close();
Now, running the above code will result in nothing at all. No zip file, or no errors. Just a completed script that apparently has done everything it was meant to do.
The Solution
In the code above can you tell what’s wrong? No, it’s not me making a typo. The second image we’re adding isn’t a valid image and doesn’t exist at all. As a result we’re not left with a zip file, but a sense of confusion which, for me at least, lasted a couple of hours.
To fix the problem we have one of two options:
1. Fix the path to the broken image,
or
2. Do a check that the file exists before trying to add it. This approach will involce amending the code to read something like so:
- // Initiate a new instance of ZipArchive
- $zip = new ZipArchive();
- // Open a new zip file
- $res = $zip->open("myZip.zip", ZipArchive::CREATE);
- // Add a file to the zip file
- if (file_exists("/path/to/file/myPhoto1.jpg"))
- {
- $zip->addFile("/path/to/file/myPhoto1.jpg", "newPhoto1.jpg");
- }
- // Add a file to the zip file
- if (file_exists("/path/to/file/myPhoto2.jpg44"))
- {
- $zip->addFile("/path/to/file/myPhoto2.jpg44", "newPhoto2.jpg");
- }
- // Add a file to the zip file
- if (file_exists("/path/to/file/myPhoto3.jpg"))
- {
- $zip->addFile("/path/to/file/myPhoto3.jpg", "newPhoto3.jpg");
- }
- // Close the zip file.
- // At this point it should be created
- $zip->close();
I personally went for the second option and after making the changes above, I was presented with the zip that I expected.
0 comments:
Post a Comment