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

March 22nd, 2013 - Posted by Steve Marks to PHP, Web Development.

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.

Tags: ,
This entry was posted on Friday, March 22nd, 2013 at 7:28 pm by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.
Comments...

Fear not, we won't publish this

Comments (2)
  1. The workaround above (file_get_contents) is very dangerous if you pack large files. (see memory limit). Close/open the zip archive periodically instead of using file_get_contents().

  2. To overcome this shortcoming, contemporary products such as PentaZip implemented strong zip encryption by encrypting .ZIP archives into a different file format.