views:

1128

answers:

4

Hi,

I have a php form that has a bunch of checkboxes that all contain links to files. Once a user clicks on which checkboxes (files) they want, it then zips up the files and forces a download.

I got a simple php zip force download to work, but when one of the files is huge or if someone lets say selects the whole list to zip up and download, my server errors out.

I understand that I can increase the server size, but are there any other ways?

Thanks!

A: 

Depending on how dynamic the content is, and how many combinations you have, you could simply have a zip file pre-built for each combination, then just return that to the user. This could get old fast though, because with 5 options, you'd have to maintain 32 different archives.

mwalling
hey thanks, so I actually thought of that, but it's not going to work. any other ideas?
You also never mentioned if you were reaching max_execution_time, or exhausting the memory_limit. If you're exhausting m_e_t, running pkzip from a system() call is still going to count on the clock.
mwalling
A: 

Is it your server running out of memory or does it not allow you to send the end result to the client? If it is just running out of memory, run the zipping on the file system instead. You can execute system commands in PHP with

system("...command...");

So you could, based on the user selection, aggregate the selected files in a temporary directory (using the system call to do file copying), and then zip the temporary directory (using the system call to call pkzip), and then ask PHP to send the temporary file to the client, after which your PHP script can delete it.

In this way, the zipping does not consume memory in your PHP application.

antti.huima
wow! thanks, I'm sort of confused. How much for you to make this work with a simple checkbox file zip via all that you said above?
A: 

You could turn on gzip at the web server/php level and skip compression in the PHP script. Then it just becomes a matter of outputting the right zip file headers in between calls to readfile().

Ant P.
+2  A: 

If you really need it to go through PHP, best idea is to use streams. With streams, memory requirements are very low and they don't grow with the size of the content.

Milan Babuškov