In linux, zlib must be enabled. The following describes how to enable this function.
1. Download and install zlib
Http://www.gzip.org/zlib/
Decompress the tar package and enter the directory to execute the command:
./Configure & make install
2. Add the php extension with the following command:
[Root @ Ansen zlib] # pwd
/Root/lnmp/php-5.3.0/ext/zlib # php source package directory
[Root @ Ansen zlib] # mv config0.m4 config. m4
[Root @ Ansen zlib] #/usr/local/php5/bin/phpize
[Root @ Ansen zlib] #./configure -- with-php-config =/usr/local/php/bin/php-config
[Root @ Ansen zlib] # make
[Root @ Ansen zlib] # cp modules/zlib. so/usr/local/php/lib/php/extension/# php extension directory
[Root @ Ansen zlib] # vi/usr/local/php5/lib/php. ini
Add extension = zlib. so
3. Restart apache
Php online compression package rar code
<? Php
$ Filename = "./zip/". date ('ymmdh'). ". zip"; // name of the generated compressed package (including path)
// Generate a file
$ Zip = new ZipArchive (); // you must enable zlib for linux,
If ($ zip-> open ($ filename, ZIPARCHIVE: CREATE )! = TRUE ){
Exit ('file cannot be opened, or file creation failed ');
}
$ FileNameArr = array ('/upload/20140606/20140606011813 -0.jpg', '/upload/20140606/20140606013717 -0.jpg ');
Foreach ($ fileNameArr as $ val ){
$ Zip-> addFile ($ val, basename ($ val); // The second parameter is the name of the file in the compressed package. Check whether the file already exists.
}
$ Zip-> close (); // close
// Send a prompt to the browser for download;
Header ("Cache-Control: max-age = 0 ");
Header ("Content-Description: File Transfer ");
Header ('content-disposition: attachment; filename = '. basename ($ filename); // file name
Header ("Content-Type: application/zip"); // zip format
Header ("Content-Transfer-Encoding: binary"); // tells the browser that the binary file
Header ('content-Length: '. filesize ($ filename); // tell the browser about the file size
Readfile ($ filename); // output file;
?>