5

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?

halfpastfour.am
  • 5,162
  • 2
  • 37
  • 58

5 Answers5

3

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
  • 55,232
  • 47
  • 119
  • 176
  • While these statements about Streams are true for their use cases, Streams cannot fully solve this problem. They are useful where PHP needs to process stream-y data in the course of script execution, but in terms of overall server behaviour, you’re still going to end up throwing a huge lump of data in memory at Apache all at once. – Benji XVI Dec 05 '10 at 04:51
2

This thread may help LAMP: How to create .Zip of large files for the user on the fly, without disk/CPU thrashing

Community
  • 1
  • 1
James Dunmore
  • 1,160
  • 9
  • 15
1

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
  • 23,825
  • 2
  • 50
  • 67
  • 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? –  Feb 16 '09 at 08:25
0

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().

-1

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
  • 1,657
  • 1
  • 19
  • 25
  • hey thanks, so I actually thought of that, but it's not going to work. any other ideas? –  Feb 16 '09 at 07:55
  • 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 Feb 16 '09 at 15:52