Use php to archive large files

I have a php form that has a set of checkboxes that contain links to files. As soon as the user clicks on which checkboxes (files) they want, he then zips up the files and forces them to download.

I have a simple php zip force download to work, but when one of the files is huge or someone allows me to select the entire list to zip up the ZIP archive and load, my server errors.

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

+4
source share
5 answers

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

+3
source

Is memory running out on your server or doesn’t allow sending the final result to the client? If it just doesn't have enough memory, run zip on the file system instead. You can execute system commands in PHP with

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

Thus, you can, based on the user's choice, aggregate the selected files in a temporary directory (using a system call to copy files), and then pin a temporary directory (using a system call to call pkzip), and then ask PHP to send a temporary file to the client, after causing your PHP script to remove it.

Thus, zipping does not consume memory in your PHP application.

0
source

You can enable gzip at the web server / php level and skip the compression in a PHP script. Then it's just a matter of outputting the correct zip file headers between readfile() calls.

0
source

Depending on how dynamic the content is and how many combinations you have, you can simply create a zip file previously created for each combination, and then simply return it to the user. It may become old fast, though, since with 5 options you will have to support 32 different archives.

-1
source

All Articles