PHP zipping algorithm

zip.thumbnail

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

7 Comments

7 Responses to “PHP zipping algorithm”

  1. Dante July 1, 2007 at 1:41 am #

    I like your site

  2. rofovnifo July 1, 2007 at 4:46 pm #

    Hi all!

    Looks good! Very useful, good stuff. Good resources here. Thanks much!

    Bye

  3. MiramarDesign March 12, 2008 at 3:21 pm #

    I’ve always wondered / wanted to do this w/ a download but never seen a library to do it. Thanks!

  4. den March 26, 2008 at 4:58 pm #

    Thanks, i try use it.

  5. shobn March 31, 2008 at 4:12 pm #

    Thanks fpr the tutorial mate..I am trying to learn PHP!!!

  6. gisho June 24, 2008 at 9:27 pm #

    how about zipping an auto generated dynamic content e.g. a .php page?

  7. Manjula January 10, 2009 at 12:46 pm #

    excellent.. its very useful to me.. thanks a lot! :)

Leave a Reply

More in php source code (30 of 31 articles)


Hello my php friends, I have written a class library using gb for a lot of image functions: Resize ...