How to zip files in the spaces (bucket)?

October 1, 2018 531 views
DigitalOcean Ubuntu 16.04

My website is made in laravel (PHP framework). There are a set of files which I need to zip and give to the user at run time.

So, one user uploads a bunch of files and another user shows up to download them. I need to zip and set the download. Can you please tell me if there’s any command which I can run via PHP code.

1 Answer

It depends on how often this is called and how intricate you want to make it.

A simple solution would be to download those files through PHP to a local temporary directory. Then you would issue a gzip command on the commandline which you can do through the PHP system() function. After the file is created you can then send that file to the user.

Reading in the output and returning that along with the proper headers to specify the format.

After the file has been received you can then clean out that directory. You can either have the file deleted in PHP after the transfer is complete, or you can setup a crontab on that directory to delete any files that are over “x” amount of time old. Depending on the size of the zip files and how long it takes transfer you can set them to be deleted every 24 hours or so forth.

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!