PHP generates a compressed file instance _php instance

Source: Internet
Author: User
Tags file copy strcmp ziparchive custom name

Approximate requirements:

Each order has multiple file attachments, and when downloading, you want to automatically package the files of the current order into a compressed package download

Detail requirements: Current order number _ month/year date + time. zip For example:

1. Generate compressed file, compressed file name format:

2. Compressed file stored in the root directory/upload/zipfile//Custom compressed file name. zip

3. Click to download the package, the system began packaging compressed files, packaging completed automatically start downloading

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

For specific operating modes, see the following code:

File path:

Compressed package File Store Path:/upload/zipfile/

Uploaded attachment store path:/upload/file/

1. Basic Profile file config.inc.php placed in 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. Compression package program code file getzip.php

Header ("content-type:text/html;
Charset=utf-8 "); Require_once '.. /config.inc.php '; Load configuration path configuration file $arrfiles = Array (sys_upload. '/1.jpg ', sys_upload. '/x.jpg ',); Here is the attachment of the file array $orderNum = ' 888 '; Order number $downFileName = ' tieniu.zip '; If the file name of the download is empty then it is the system custom name $ZIPURL = Create_zip ($arrfiles, $orderNum) if specified; Generated compressed file noun file_down ($zipUrl, $downFileName); Provides HTTP downloads, and can rename download files, recommend renaming, prevent path guessing/* Generate cabinet file name * @param [string] $orderNum order number * @return [string] Returns a compressed text with an absolute path order number Part name/function Get_zipname ($orderNum) {$zipName = Sys_download. '/' . Date (' Ym '). '/' . $orderNum. '_' . Date ("Ymd_hi").
  '. zip ';
return $zipName; * * * Packaging The package's directory structure according to specific requirements/function Pack_object () {} * * Generate a compressed package * @param [array] $arrfiles an array of files with an absolute path * @pa Ram [string] $orderNum order number * @return [string] Returns a compressed file name for an order number with an absolute path if failure returns FALSE/function Create_zip ($arrfiles, $order Num) {$zipName = Get_zipname ($orderNum);//Get filename dir_create (dirname ($zipName);//Create a directory $zip to generate compressed files = new zipArchive ();
  if ($zip->open ($zipName, ziparchive::create)!== TRUE) {return FALSE; foreach ($arrfiles as $path) {if (Is_file ($path)) {//To determine whether the file exists $zip->addfile ($path, basename ($path));
  The file is added to the compression packet}} $zip->close ();
return $zipName; * * * Processing file directory * @param [array] $arrfiles an array of files with an absolute path * @param [string] $dirpath file path * @return [string] Return the processed file path to facilitate
  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_d
  IR ($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 * @param [string] $to Copy File destination * @return [Bool] successful 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 processing function * @param [string] $file file path * @param [string] $filename the name of the file renamed at download time * @param [string] $data download file
  Loaded 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 $data;
  else {ReadFile ($file);
} exit;

function File_ext ($filename) {return Strtolower (Trim (substr ($filename, '. '), 1))} This function is not used to do a package download function for the entire directory 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.PHP program to generate compressed files need to use the compression class: Ziparchive

This is the extension of PHP, since the php5.2 version has supported this extension, if you are in use when there are errors, look at the php.ini inside the EXTENSION=PHP_ Zip.dll before the semicolon is removed, and then restart Apache so you can use this class library.

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.