×

Use PHP To Zip Folders For Download

zip_files

Published: 2010-09-23

Uploading files with PHP is covered to death on the internet. The other side of that, downloading files, is not covered nearly as much.

When a user uploads say, hundreds of images to a folder, and they want to download them all later, it can turn into a giant time sink making them download the files one at a time.

A better alternative would be to offer the files individually and with a "Download All As Zip" link.

I found a solution here, but it required putting the php file in every directory you would like to zip. Not exactly the best solution. So I tweaked it to be a bit more dynamic.

This code is based on the assumption that there is a folder called something like user_uploads with a sub-folder for each user, something like Travis_Berry

Save this code as zip_folders.php

Then when you want to provide a link to download a users uploads as a zip file, you link to it like,

That will cause a download prompt to appear for the user when clicking on the link.

You'll notice I use the system Zip command as opposed to the PHP Zip extension. For more complex Zipping needs, it looks like it would be a better alternative. For something simple like this, I find the system call sufficient.

If you find this post useful, or have any PHP Zip tips, let me know in the comments.