Create ZIP file archives on the fly-PHP

May 29th, 2009 by vidyadhar.yagnik § 7

In some website there is need of downloading functionality of multiple files. In that case it is headache for PHP developer that how to give option for download multiple file in single click with lower bandwidth. So, the solution is here: just create zip file containing all the files and give one link for download the zip files.

createZip.inc.php is a class which creates zip file from different files stored on various directories on server and output the necessary request response headers to serve the generated ZIP archive for download.

Example Code Snippet:

include_once("createZip.inc.php");
$createZip = new createZip;
$directoryName = "DownloadImages/";

$createZip -> addDirectory($directoryName); // Adding Directory to ZIP file

$file1ToZip = “image1.jpeg”; // filename1 which is to be added in zip
$fileContents=file_get_contents($file1ToZip);
$createZipFile->addFile($fileContents, $directoryName.$file1ToZip);

$file2ToZip = “image2.jpeg”; // filename2 which is to be added in zip
$fileContents=file_get_contents($file2ToZip);
$createZipFile->addFile($fileContents, $directoryName.$fileToZip);

$outputDir = “./”;
$outputFile = $outputDir.”Images.zip”; // Zip file name

$fd = fopen ($outputFile, “wb”); // Temporary Creating file on disk
$out = fwrite ($fd, $createZip -> getZippedfile()); // Writing zip contents to temporary file
fclose ($fd);

$createZip -> forceDownload($outputFile); // stream the generated ZIP archive for download

@unlink($outputFile); // unlink temporary file

Credits goes to Author of the class: “Rochak Chauhan”
Source: http://www.phpclasses.org/browse/package/2322.html

Author:
Vidyadhar Yagnik
PHP Developer: Digicorp Information Systems Pvt. Ltd.
[email protected]

§ 7 Responses to “Create ZIP file archives on the fly-PHP”

What's this?

You are currently reading Create ZIP file archives on the fly-PHP at Digicorp.

meta

Share