Jar File package php Zip package download file

Source: Internet
Author: User
Tags fread ziparchive
 
  
  
  1. Package files into a zip format
  2. Features for downloading files

Key points of Analysis:

  1. Here I'm using PHP's own ziparchive class

A) We only need to new a Ziparchive object, then use the open method to create a zip file, and then use the AddFile method, the file will be packaged to write to the zip file just created, it is best to remember to close the object.

b) Note: When using the Open method, the second parameter, $flags, is optional, $flags used to specify how to handle the open zip file in four different cases

I. Ziparchive::overwrite always creates a new file, and if the specified zip file exists, it will overwrite the

II. ziparchive::create If the specified zip file does not exist, create a new

III. ziparchive::excl if the specified zip file exists, an error is given

iv.. Ziparchive::checkcon

$nid =intval($this_request->getparam (' nid '));$filelist = $thisModel->getoverenter ($nid);foreach($filelist as$f) {$row [' file '] =ROOT.'/public/upload/logo/'. $f [' Ne_file '];$row [' name '] = $f [' Es_name '];$ext =Explode('.',$f [' Ne_file ']);$row [' ext '] = $ext [1];$FILENAMEARR []= $row;} $name =Date(' Ymdhis ').'. zip ';$filename =ROOT."/public/upload/". $name; // the resulting file name (with path)$zip =NewZiparchive ();if(!file_exists($filename)) {$zip->open ($filename, ziparchive::CREATE);//Create an emptyZipfile for($i =0; $i <Count($FILENAMEARR); $i + +) {$zip->addfile ($fileNameArr [$i] [' file '], Iconv(' UTF-8 ',' GB2312 ', $FILENAMEARR [$i] [' name ']).'.'. $FILENAMEARR [$i] [' ext ']);} $zip->close ();//Open Fileif(file_exists($filename)) {$file =fopen($filename,"R");Header ("Content-type:application/octet-stream");Header ("Accept-ranges:bytes");Header ("Accept-length:".filesize($filename));Header ("content-disposition:attachment; Filename= ". $name);Echofread($file, filesize($filename));$buffer =1024x768; while(!feof($file)) {$file _data=fread($file,$buffer);Echo$file _data;}fclose($file);}Else{Echo"";}}unlink($filename);Exit();

The above describes the jar file package php Zip package download file, including the contents of the jar file packaging, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    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.