A few lines of code to easily implement PHP file package download zip code instance details

Source: Internet
Author: User
Tags php ziparchive zip extension ziparchive
This article is mainly for you to introduce in detail the use of PHP zlib function, easy to implement PHP file package download zip, with a certain reference value, interested in small partners can refer to

The example of this article for everyone to share the php file package download zip code for everyone to refer to, the specific content as follows



<?php//Get file List function List_dir ($dir) {$result = array ();   if (Is_dir ($dir)) {$file _dir = Scandir ($dir); foreach ($file _dir as $file) {if ($file = = '. ' | | $file = = '.. ')    {continue; } elseif (Is_dir ($dir. $file)) {$result = Array_merge ($result, List_dir ($dir. $file. '    /'));    } else{Array_push ($result, $dir. $file);  }}} return $result; }//Gets the list $datalist =list_dir ('.. /'); $filename = "./bak.zip"; The resulting file name (with path) if (!file_exists ($filename)) {//Regenerate file $zip = new ziparchive ();//Use this class, Linux needs to turn on Zlib,windows to cancel Php_zip.  DLL before the comment if ($zip->open ($filename, ziparchive::create)!==true) {exit (' cannot open file, or file creation failed '); } foreach ($datalist as $val) {if (file_exists ($val)) {$zip->addfile ($val, basename ($val));//The second parameter is placed in a compressed package The file name in, if the file may be duplicated, you need to pay attention to}} $zip->close ();//close} if (!file_exists ($filename)) {exit ("Cannot find File");//Even if created,  It is still possible to fail .... } header ("Cache-control:public"); Header ("Content-description:file Transfer"); Header(' Content-disposition:attachment; Filename= '. basename ($filename)); FileName header ("Content-type:application/zip"); Header in zip format ("content-transfer-encoding:binary"); Tell the browser that this is the header of the binary file (' content-length: '. FileSize ($filename)); Tell the browser, file size @readfile ($filename);? >


PHP ziparchive is a PHP extension class, you can easily achieve zip file compression and decompression, before using the first to ensure that the PHP zip extension has been opened, the specific opening method here does not say, different platforms to open the method of PHP amplification online have, if there is doubt welcome communication.

Here's a general example of compressing and decompressing files using PHP ziparchive for reference.

First, unzip the zip file


$zip =new ziparchive;//Create a new Ziparchive object if ($zip->open (' Test.zip ') ===true) {$zip->extractto (' images ');// Assume that the decompression is within the current path under the Images folder $zip->close ();//close processed zip file}

Compress the file into a zip file

$zip =new ziparchive;if ($zip->open (' Test.zip ', ziparchive::overwrite) ===true) {$zip->addfile (' image.txt ');// Assume that the file name is Image.txt, under the current path $zip->close ();}


Third, file append content to zip file


$zip =new ziparchive; $res = $zip->open (' Test.zip ', ziparchive::create), if ($res ===true) {$zip->addfromstring (' Test.txt ', ' file content goes here '); $zip->close (); echo ' OK ';} else{echo ' failed ';}


Iv. Package a folder into a zip file


function Addfiletozip ($path, $zip) {$handler =opendir ($path);//Open the current folder is specified by $path. while (($filename =readdir ($handler))!==false) {if ($filename! = "." && $filename! = "...") {//Folder file name is '. ' and '. ', do not operate on them  if (Is_dir ($path. ") /". $filename)) {//If an object being read is a folder, the recursive  addfiletozip ($path." /". $filename, $zip);  } else{//Add files to the Zip object  $zip->addfile ($path. " /". $filename);  } }} @closedir ($path);} $zip =new ziparchive (); if ($zip->open (' Images.zip ', ziparchive::overwrite) = = = TRUE) {addfiletozip (' images/', $zip) ; Call the method, manipulate the root directory to be packaged, and pass the Ziparchive object to the method $zip->close (); Close the processed zip file}

The above is a few lines of code easy to implement PHP file package download zip Code instance details of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.