Function Code in zip format for php compressed files

Source: Internet
Author: User
Tags ziparchive
Function Code in zip format for php compressed files

  1. /* @ Creates a compressed zip file: function for compressing multiple files into one zip file
  2. * @ $ Files array type instance array ("1.jpg"," 2.jpg ");
  3. * @ Destination the path of the target file, for example, "c:/androidyue.zip"
  4. * @ $ Overwrite indicates whether to overwrite the same object as the target object.
  5. * @ Site http://bbs.it-home.org
  6. */
  7. Function create_zip ($ files = array (), $ destination = '', $ overwrite = false ){
  8. // If the zip file already exists and overwrite is false, return false
  9. // If the zip file already exists and is set to not overwrite, false is returned.
  10. If (file_exists ($ destination )&&! $ Overwrite) {return false ;}
  11. // Vars
  12. $ Valid_files = array ();
  13. // If files were passed in...
  14. // Obtain the real and valid file name
  15. If (is_array ($ files )){
  16. // Cycle through each file
  17. Foreach ($ files as $ file ){
  18. // Make sure the file exists
  19. If (file_exists ($ file )){
  20. $ Valid_files [] = $ file;
  21. }
  22. }
  23. }
  24. // If we have good files...
  25. // If a real and valid file exists
  26. If (count ($ valid_files )){
  27. // Create the archive
  28. $ Zip = new ZipArchive ();
  29. // Open the file and overwrite it if it already exists. otherwise, create
  30. If ($ zip-> open ($ destination, $ overwrite? ZIPARCHIVE: OVERWRITE: ZIPARCHIVE: CREATE )! = True ){
  31. Return false;
  32. }
  33. // Add the files
  34. // Add a file to the compressed file
  35. Foreach ($ valid_files as $ file ){
  36. $ Zip-> addFile ($ file, $ file );
  37. }
  38. // Debug
  39. // Echo The zip archive ins, $ zip-> numFiles, 'files with a status of ', $ zip-> status;
  40. // Close the zip -- done!
  41. // Close the file
  42. $ Zip-> close ();
  43. // Check to make sure the file exists
  44. // Check whether the file exists
  45. Return file_exists ($ destination );
  46. } Else {
  47. // If no valid file exists, false is returned.
  48. Return false;
  49. }
  50. }
  51. /****
  52. // Test the function
  53. $ Files = array ('temp. php', 'Test. php ');
  54. Create_zip ($ files, 'myzipfile.zip ', true );
  55. ****/
  56. ?>

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.