We can easily archive remote files on our server and put the archive for download by our users.
Here’s a code snippet:
1 2 3 4 5 6 7 8 9 10 11 |
<?php $url = "http://www.google.com/intl/en_ALL/images/logo.gif"; $zip = new ZipArchive; if ($zip->open('test.zip', ZipArchive::CREATE) === TRUE) { $zip->addFromString('logo.gif', file_get_contents($url)); $zip->close(); echo 'ok'; } else { echo 'failed'; } ?> |
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 🙂