问题
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?
回答1:
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.
回答2:
This thread may help LAMP: How to create .Zip of large files for the user on the fly, without disk/CPU thrashing
回答3:
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.
回答4:
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()
.
回答5:
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.
来源:https://stackoverflow.com/questions/552516/use-php-to-zip-large-files