PHP zipping algorithm
- 6 Comment
![]()
How can we zip two files and save output as output.zip. For example, in gmail you can download all the attachment as a single zip file. How can we write that code using php.
Click more for Source Code:
/**
* Php code for file zip
* Author : Sajith.M.R
* Date: 02-02-2007
*/
/** Include the Pear Library for Zip */
include ('Archive/Zip.php');
/** Create a Zipping Object...
* Name of zip file to be created..
* You can specify the path too */
$obj = new Archive_Zip('test.zip');
/**
* create a file array of Files to be Added in Zip
*/
$files = array('black.gif',
'blue.gif',
);
/**
* creating zip file..if success do something else do something...
* if Error in file creation ..it is either due to permission problem (Solution: give 777 to that folder)
* Or Corruption of File Problem..
*/
if ($obj->create($files)) {
// echo 'Created successfully!';
} else {
//echo 'Error in file creation';
}
?>; // We'll be outputting a ZIP
header('Content-type: application/zip');
// It will be called test.zip
header('Content-Disposition: attachment; filename="test.zip"');
//read a file and send
readfile('test.zip');
?>;
//This will output test.zip to the browser. If you dont want zip file as output (just saving into directory) leave the second part of above code
//This will output test.zip to the browser. If you dont want zip file as output (just saving into directory) leave the second part of above code
Note:
Download the pear library from: http://download.pear.php.net/package/Archive_Zip-0.1.1.tgz
or
install via pear pear install http://download.pear.php.net/package/Archive_Zip-0.1.1.tgz
6 Comments on this post
Trackbacks
-
Dante said:
I like your site
July 1st, 2007 at 1:41 am -
rofovnifo said:
Hi all!
Looks good! Very useful, good stuff. Good resources here. Thanks much!
Bye
July 1st, 2007 at 4:46 pm -
MiramarDesign said:
I’ve always wondered / wanted to do this w/ a download but never seen a library to do it. Thanks!
March 12th, 2008 at 3:21 pm -
den said:
Thanks, i try use it.
March 26th, 2008 at 4:58 pm -
shobn said:
Thanks fpr the tutorial mate..I am trying to learn PHP!!!
March 31st, 2008 at 4:12 pm -
gisho said:
how about zipping an auto generated dynamic content e.g. a .php page?
June 24th, 2008 at 9:27 pm




