PHP functions for compressing files into ZIP files

Source: Internet
Author: User
Tags ziparchive

PHP functions for compressing files into ZIP files

<? Php <br/>/* @ creates a compressed zip file: function for compressing multiple files into one ZIP file <br/> * @ $ files array-type instance array ("1.jpg ", "2.jpg"); <br/> * @ destination the path of the target file, for example," C: /androidyue.zip "<br/> * @ $ overwrite indicates whether to overwrite the same file as the target file. <br/> * @ recorded by androidyue <br/> * @ blog: http://thinkblog.sinaapp.com <br/> */<br/> function create_zip ($ files = array (), $ Destination = '', $ overwrite = false) {<br/> // If the ZIP file already exists and overwrit E is false, return false <br/> // If the ZIP file already exists and is set to not overwrite, false is returned. <br/> If (file_exists ($ destination )&&! $ Overwrite) {return false ;}< br/> // vars <br/> $ valid_files = array (); <br/> // If files were passed in... <br/> // get the real and valid file name <br/> If (is_array ($ files )) {<br/> // cycle through each file <br/> foreach ($ files as $ file) {<br/> // make sure the file exists <br/> If (file_exists ($ file) {<br/> $ valid_files [] = $ file; <br/>}< br/> // if we have good files... <br/> // if a valid file exists <br/> If (count ($ valid_f Iles) {<br/> // create the archive <br/> $ zip = new ziparchive (); <br/> // open the file and overwrite it if the file already exists, if not, create <br/> if ($ zip-> open ($ destination, $ overwrite? Ziparchive: overwrite: ziparchive: Create )! = True) {<br/> return false; <br/>}< br/> // Add the files <br/> // Add a file to the compressed file <br/> foreach ($ valid_files as $ file) {<br/> $ zip-> AddFile ($ file, $ file ); <br/>}< br/> // debug <br/> // echo the ZIP Archive ins INS, $ zip-> numfiles, 'files with a status ', $ zip-> status; <br/> // close the zip -- done! <Br/> // close the file <br/> $ zip-> close (); <br/> // check to make sure the file exists <br/> // check whether the file exists <br/> return file_exists ($ destination ); <br/>} else {<br/> // returns false if no valid file exists. <br/> return false; <br/>}< br/>/***** <br/> // test the function <br/> $ files = array ('temp. php', 'test. PHP '); <br/> create_zip ($ files, 'myzipfile.zip', true); <br/> *****/<br/>?> 

From http://thinkblog.sinaapp.com Open Source

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.