Creating a zip file with PHP's ZipArchive

I recently had a requirement to create a zip file from a number of files created within my application.

As it has been years since I last had this problem, I had a look around and discovered that PHP 5.2 has the ZipArchive class that makes this easy. ZipArchive is fully featured, but as I just wanted to create a simple zip file, all that I needed to do was:

Note that I can set the filename and any subdirectories within the zip file completely independently of the name and location of the file that I am adding which is useful.

The file '' is now ready for downloading which currently I do using:

Now, the next time I need to do this, I'll remember what I did!