Archiving Remote Files Using PHP

We can easily archive remote files on our server and put the archive for download by our users.

Here’s a code snippet:

It uses the ZipArchive built in class of PHP to archive the target file which is stored on a remote host. It reads the remote file using “file_get_contents()” function and uses the “addFromString()” method of the ZipArchive object to add the data as a local file inside the archive. The first parameter of this method is the name of the file which will be inside the archive. This file is usually called “local file”. The second parameter is the file contents in raw string format. Instead of the file_get_contents(), we could use alternative fopen(), fread() or fgets() functions to get the file contents as string. But when we have better and quicker solution, why should we go for complex ones ?

Enjoy 🙂


5 Comments Archiving Remote Files Using PHP

Leave a Reply

Your email address will not be published. Required fields are marked *