Example of php online compression package rar and automatic file download

Source: Internet
Author: User
Tags file size php online rar zip ziparchive

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;
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.