Monday, 16 July 2018

PHP’s ZipArchive Class Not Creating Zip File (And No Errors)

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:

  1. // Initiate a new instance of ZipArchive  
  2. $zip = new ZipArchive();  
  3.   
  4. // Open a new zip file  
  5. $res = $zip->open("myZip.zip", ZipArchive::CREATE);  
  6.   
  7. // Add a file to the zip file  
  8. $zip->addFile("/path/to/file/myPhoto1.jpg""newPhoto1.jpg");  
  9.   
  10. // Add a file to the zip file  
  11. $zip->addFile("/path/to/file/myPhoto2.jpg44""newPhoto2.jpg");  
  12.   
  13. // Add a file to the zip file  
  14. $zip->addFile("/path/to/file/myPhoto3.jpg""newPhoto3.jpg");  
  15.   
  16. // Close the zip file.  
  17. // At this point it should be created  
  18. $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:

  1. // Initiate a new instance of ZipArchive  
  2. $zip = new ZipArchive();  
  3.   
  4. // Open a new zip file  
  5. $res = $zip->open("myZip.zip", ZipArchive::CREATE);  
  6.   
  7. // Add a file to the zip file  
  8. if (file_exists("/path/to/file/myPhoto1.jpg"))  
  9. {  
  10.     $zip->addFile("/path/to/file/myPhoto1.jpg""newPhoto1.jpg");  
  11. }  
  12.   
  13. // Add a file to the zip file  
  14. if (file_exists("/path/to/file/myPhoto2.jpg44"))  
  15. {  
  16.     $zip->addFile("/path/to/file/myPhoto2.jpg44""newPhoto2.jpg");  
  17. }  
  18.   
  19. // Add a file to the zip file  
  20. if (file_exists("/path/to/file/myPhoto3.jpg"))  
  21. {  
  22.     $zip->addFile("/path/to/file/myPhoto3.jpg""newPhoto3.jpg");  
  23. }  
  24.   
  25. // Close the zip file.  
  26. // At this point it should be created  
  27. $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