PHP Download Compressed package file

Source: Internet
Author: User
Tags php download ziparchive

PHP compressed files require ZipArchive  classes, and Windows environments need to open php_zip.dll extensions.

Compress files
$zip=Newziparchive ();//open a Zip document, Ziparchive::overwrite: If such a document exists, overwrite; Ziparchive::create: If it does not exist, create$res=$zip->open (' Test.zip ', Ziparchive::overwrite | Ziparchive::CREATE);if($res){    //adding a.txt to compressed documents    $zip->addfile (' A.txt '); //Add a string to the b.txt in the compressed document    $zip->addfromstring (' b.txt ', ' This is B.txt '); //Add an empty directory B to the zipped document    $zip->addemptydir (' B ');}//Close an open compressed document$zip->close ();
Compressed directory
1 /**2 * @param $dir destination directory path3 * @param $zip ziparchive class object4 * @param $prev5  */6 functionCompressdir ($dir,$zip,$prev= '. ')7 {8     $handler=Opendir($dir);9     $basename=basename($dir);Ten     $zip->addemptydir ($prev. ‘/‘ .$basename); One      while($file=Readdir($handler)) A     { -         $realpath=$dir. ‘/‘ .$file; -         if(Is_dir($realpath)) the         { -             if($file!== '. ' &&$file!== ' ... ') -             { -                 $zip->addemptydir ($prev. ‘/‘ .$basename. ‘/‘ .$file); +Compressdir ($realpath,$zip,$prev. ‘/‘ .$basename); -             } +}Else A         { at             $zip->addfile ($realpath,$prev. ‘/‘ .$basename. ‘/‘ .$file); -         } -     } -  -     Closedir($handler); -     return NULL; in } -  to $zip=Newziparchive (); + $res=$zip->open (' Test.zip ', Ziparchive::overwrite | Ziparchive::CREATE); - if($res) the { *Compressdir ('./test ',$zip); $     $zip-close ();Panax Notoginseng}
Unzip
$zip New ziparchive (); $res $zip->open (' Test1.zip '); if ($res) {    //  unzip the file to the specified directory    $zip->extractto (' test ') );     $zip-Close ();}
Download the compressed package

To download a compressed package, you need to compress the target directory, then download the compressed package, and finally delete the compressed package.

In the compressed directory example, append the following code:

Header (' Content-type:text/html;charset=utf-8 '); Header (' Content-disposition:attachment;filename=test.zip '); $filesize filesize ('./test.zip '); ReadFile ('./test.zip '); Header (' Content-length: '. $filesize ); unlink ('./test.zip ');

  

PHP Download Compressed package 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.