Zipping files on the server

I need to implement some functionality on my site that allows the use to create a library of files and then click a button to download them.

Some of the files are fairly large so the total size of all the files needing zipped could be as much as 500MB if not more.

So my first question: Is this feasible to do using PHP or would these large file sizes cause memory errors on the server? Note the site is on a VPS.

If it is possible, can you suggest how to achieve this and what the most efficient way to do it is.

Ideally I would like the zip to automatically download to the users PC when it has been created.

Any advice much appreciated.


You will likely run into a slew of issues

  1. Memory Size, definitely an issue here
  2. The browser will likely time out in the process of zipping that much data

What I have personally done in the past, is I hand off that task to a cronjob that runs every X minutes. It picks up a file that explains what needs to be done, list of files, e-mail of person requesting the download, etc. It reads the file, creates the zip using a shell script, passes the location of the zip and the e-mail address to a PHP script, that kicks an email to the user with a download link.

I then have a cleanup script that removes the zip file after X number of hours/days.

Thanks for the swift reply cpradio

Yeah I suspected there would be problems.

My site specification requires the zip files are delivered via the browser - would there be a way of creating the zip using the shell script immediately and then displaying a download link or automatically downloading the zip to the users hard drive?

You can run a system() or exec() command to build the zip file (thus releasing the Memory issue from PHP), but I still have my doubts that the zip will be ready before the browser times out on the page execution.

You can introduce a polling technique, where you use an Ajax script or a refreshing iframe to check if the process is done, and then load up the download link as well. I find that technique a lot more messy and harder to debug/maintain, but others seem to like it…

Thanks cpradio

I assume the page execution time out time is an element out of my control?

The Ajax polling does sound quite messy but it may be my only option:

Thanks for the very helpful advice

Yes (in most cases), however you can set a time out period on the server level (in .NET you can even set it on the application level, php check out this thread), but most users always expect an immediate result and would likely close the window, hit the back button, hit refresh, etc while waiting over a minute for the page to load.