PHP zipped folder with compression class

Source: Internet
Author: User
Tags ziparchive

//compressed FolderfunctionAddfiletozip ($path,$zip){    $handler=Opendir($path);//opens the current folder 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 that is read is a folder, the recursiveAddfiletozip ($path." /".$filename,$zip); }Else{//adding files to a Zip object                /** AddFile (parameter 1, parameter 2) * Parameter 1: File full path (with file name) * Parameter 2: Custom new file name (without path directly added to Compress the package root directory, otherwise the corresponding directory will be automatically created in the compressed package) **/                $zip->addfile ($path." /".$filename,$filename); }        }    }    @Closedir($path);}$zip=Newziparchive ();$dirname= '/uploads/text_dir ';//folders that need to be packaged$zip _name= ' download/'.$id.‘. Zip ';//the packaged file$res=$zip->open ($zip _name, Ziparchive::CREATE);if($res===TRUE) {Addfiletozip ($dirname,$zip);//invokes the method, operates on the root directory to be packaged, and passes the Ziparchive object to the method        $zip->close ();//close the processed zip file//Browser download        Header(' Content-type:application/zip '); Header(' Content-disposition:attachment; Filename= '.$zip _name); Header(' Content-length: '.filesize($zip _name)); ReadFile($zip _name);}Else{    Echo"<script>alert (' failed to create the package '); Location.reload ();</script>";Exit;}

PHP zipped folder with compression class

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.