PHP simple way to create zip compressed files, _php tutorial

Source: Internet
Author: User
Tags create zip getting started with php php regular expression ziparchive

PHP simple way to create a zip compressed file,


This article describes a simple way for PHP to create a zip archive. Share to everyone for your reference, as follows:

/* Creates a compressed zip file */function create_zip ($files = Array (), $destination = ", $overwrite = False) {//if the ZIP file already exists and overwrite is false, return False if (file_exists ($destination) &&! $overwrite) {retur n false;  }//vars $valid _files = Array ();  If files were passed in ... if (Is_array ($files)) {//cycle through each file foreach ($files as $file) {//make sure the file exists if (File_exists ($file))      {$valid _files[] = $file;    }}}//if we have good files ... if (count ($valid _files)) {//create the archive $zip = new ziparchive (); if ($zip->open ($destination, $overwrite?    Ziparchive::overwrite:ziparchive::create)!== true) {return false;    }//add the files foreach ($valid _files as $file) {$zip->addfile ($file, $file);    }//debug//echo ' The zip archive contains ', $zip->numfiles, ' files with a status of ', $zip->status;    Close the zip--done!  $zip->close ();  Check to make sure the file exists return file_exists ($destination);  } else {return false; }}

How to use:

$files _to_zip = Array (  ' preload-images/1.jpg ',  ' preload-images/2.jpg ',  ' preload-images/5.jpg ',  ' Kwicks/ringo.gif ',  ' rod.jpg ',  ' reddit.gif ');//if true, good; if False, zip creation Failed$result = Create_zip ( $files _to_zip, ' my-archive.zip ');

More about PHP related content readers can view this site topic: "PHP operation zip file and compression skills summary", "PHP file Operation Summary", "PHP Regular Expression Usage Summary", "Php+ajax Tips and Applications Summary", "PHP Operations and Operator Usage Summary", " PHP Network Programming Tips Summary, PHP Basic Grammar Primer Tutorial, PHP Operations Office Document Tips summary (including word,excel,access,ppt), PHP date and Time usage summary, PHP Object-oriented Programming primer tutorial, PHP String Usage Summary, "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"

I hope this article is helpful to you in PHP programming.

http://www.bkjia.com/PHPjc/1125257.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125257.html techarticle php Simple way to create a zip file, this article describes the PHP simple way to create a zip compressed file. Share to everyone for reference, as follows:/* Creates a compressed Z ...

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