PHP generates compressed file instances. _ PHP Tutorial

Source: Internet
Author: User
Tags ziparchive custom name
PHP generates a compressed file instance ,. PHP generates a compressed file instance. the general requirement is that each order has multiple file attachments, when downloading, you want to automatically package the file in the current order into a compressed package. for details about downloading, you need PHP to generate a compressed file instance,

General requirements:

Each order has multiple file attachments. during the download, you want to automatically package the files in the current order into a compressed package for download.

Detailed requirements: before the order number, such as _ year, month, and day. zip:

1. generate a compressed file. the format of the compressed file name is as follows:

2. the compressed file is stored in the root directory/upload/zipfile/year/custom compressed file name. zip

3. click to download the compressed package. The system starts to package the compressed file. after packaging, the package starts automatically.

4. to prevent exposure of the compressed package file path, you need to rename the downloaded compressed package file name.

For specific operation modes, see the following code:

File path:

Path of the compressed file:/upload/zipfile/

Path for storing uploaded attachments:/upload/file/

1. put the basic configuration file config. inc. php in the system root directory.

define('SYS_ROOT', str_replace("\\", '/', dirname(__FILE__)));define('SYS_UPLOAD', SYS_ROOT.'/upload/file');define('SYS_DOWNLOAD', SYS_ROOT.'/upload/zipfile');define('SYS_WIN', strpos(strtoupper(PHP_OS), 'WIN') !== false ? true: false);define('SYS_CHMOD', ('0777' && !SYS_WIN) ? '0777' : 0);

2. getzip. php

Header ("Content-type: text/html; charset = utf-8"); require_once '.. /config. inc. php '; // load the configuration path configuration file $ arrfiles = array (SYS_UPLOAD. '/1.jpg', SYS_UPLOAD. '/x.jpg',); // The array of attachments $ orderNum = '000000'; // Order No. $ downFileName = 'tieniu.zip '; // if the downloaded file name is empty, it indicates the custom name of the system. if it is specified, the specified name $ zipUrl = create_zip ($ arrfiles, $ orderNum) is displayed ); // the name of the generated compressed file file_down ($ zipUrl, $ downFileName); // provides http download and rename the downloaded file. we recommend that you rename the file, prevent path guessing/** generate compressed package files Name * @ param [String] $ orderNum order number * @ return [String] returns the compressed file name with an absolute path order number */function get_zipname ($ orderNum) {$ zipName = SYS_DOWNLOAD. '/'. date ('ym '). '/'. $ orderNum. '_'. date ("Ymd_Hi "). '.zip '; return $ zipName;}/** set the directory structure of the compressed package according to the specific requirements */function pack_object () {}/** generate a compressed package * @ param [Array] $ arrfiles Array of files with an absolute path * @ param [String] $ orderNum order number * @ return [String] returns an absolute path if the compression file name of the order number fails, F is returned. ALSE */function create_zip ($ arrfiles, $ orderNum) {$ zipName = get_zipname ($ orderNum); // Obtain the file name dir_create (dirname ($ zipName )); // CREATE a directory for generating compressed files $ zip = new ZipArchive (); if ($ zip-> open ($ zipName, ZIPARCHIVE: CREATE )! = TRUE) {return FALSE;} foreach ($ arrfiles as $ path) {if (is_file ($ path )) {// Determine whether the file exists $ zip-> addFile ($ path, basename ($ path )); // add the file to the compressed package }}$ zip-> close (); return $ zipName ;} /** processing file directory * @ param [Array] $ arrfiles Array of files with absolute paths * @ param [String] $ dirpath file path * @ return [String] returns the processed file path, conveniently generate the file directory */function dir_path ($ dirpath) {$ dirpath = str_replace ('\', '/', $ dirpath); if (substr ($ dirpath, -1)! = '/') $ Dirpath = $ dirpath. '/'; return $ dirpath ;} /** generate file directory * @ param [String] $ path file path * @ return [String] returns the generated file directory structure */function dir_create ($ path) {if (is_dir ($ path) return true; $ dir = str_replace (SYS_DOWNLOAD. '/', '', $ path); $ dir = dir_path ($ dir); $ temp = explode ('/', $ dir); $ cur_dir = SYS_DOWNLOAD. '/'; $ max = count ($ temp)-1; for ($ I = 0; $ I <$ max; $ I ++) {$ cur_dir. = $ temp [$ I].' /'; If (is_dir ($ cur_dir) continue; @ mkdir ($ cur_dir); if (SYS_CHMOD) @ chmod ($ cur_dir, SYS_CHMOD); if (! Is_file ($ cur_dir. '/index.html ')&&! Is_file ($ cur_dir. '/index. php ') file_copy (SYS_ROOT. '/upload/index.html', $ cur_dir. '/index.html');} return is_dir ($ path );} /** file COPY * @ param [String] $ from copy source file * @ param [String] $ to copy file destination * @ return [Bool] success ture failure false */function file_copy ($ from, $ to) {dir_create (dirname ($ to); if (is_file ($ to) & SYS_CHMOD) @ chmod ($ to, SYS_CHMOD ); if (@ copy ($ from, $ to) {if (SYS_CHMOD) @ chmod ($ To, SYS_CHMOD); return true;} else {return false ;}} /** file download handler * @ param [String] $ file path * @ param [String] $ file name renamed at Download time * @ param [String] $ data Download file-filled data content */function file_down ($ file, $ filename = '', $ data ='') {if (! $ Data &&! Is_file ($ file) exit; $ filename = $ filename? $ Filename: basename ($ file); $ filetype = file_ext ($ filename); $ filesize = $ data? Strlen ($ data): filesize ($ file); ob_end_clean (); @ set_time_limit (0); if (strpos ($ _ SERVER ['http _ USER_AGENT '], 'msie ')! = False) {header ('cache-Control: must-revalidate, post-check = 0, pre-check = 0'); header ('pragma: public ');} else {header ('pragma: no-cache');} header ('expires :'. gmdate ('d, d m y h: I: s '). 'gmt'); header ('content-Encoding: none'); header ('content-Length :'. $ filesize); header ('content-Disposition: attachment; filename = '. $ filename); header ('content-Type :'. $ filetype); if ($ data) {echo $ d Ata;} else {readfile ($ file);} exit;} function file_ext ($ filename) {return strtolower (trim (substr (strrchr ($ filename ,'. '), 1);} // This function is not used, used to package the entire directory and download function listdir ($ start_dir = '. ') {$ files = array (); if (is_dir ($ start_dir) {$ fh = opendir ($ start_dir ); while ($ file = readdir ($ fh ))! = False) {if (strcmp ($ file ,'. ') = 0 | strcmp ($ file ,'.. ') = 0) continue; $ filepath = $ start_dir. '/'. $ file; if (is_dir ($ filepath) $ files = array_merge ($ files, listdir ($ filepath); else array_push ($ files, $ filepath );} closedir ($ fh);} else {$ files = false;} return $ files ;}

3. the compression class is used to generate a compressed file by the PHP program: ZipArchive

This is an extension class of php. it has been supported since php5.2. if you encounter an error while using it, check php. in ini, have you removed the semicolon before extension = php_zip.dll, and then restart Apache to use this class library.

Renewal, general requirement: each order has multiple file attachments. when downloading the file, you want to automatically package the file in the current order into a compressed package. download details are required...

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.