PHP online compression zip function code

Source: Internet
Author: User
Tags create zip php online ziparchive
  1. /* PHP Create Zip Compressed package */
  2. function Create_zip ($files = Array (), $destination = ", $overwrite = False) {
  3. If the zip file already exists and overwrite is false, return false
  4. if (file_exists ($destination) &&! $overwrite) {return false;}
  5. VARs
  6. $valid _files = Array ();
  7. If files were passed in ...
  8. if (Is_array ($files)) {
  9. Cycle through each file
  10. foreach ($files as $file) {
  11. Make sure the file exists
  12. if (file_exists ($file)) {
  13. $valid _files[] = $file;
  14. }
  15. }
  16. }
  17. If we have good files ...
  18. if (count ($valid _files)) {
  19. Create the Archive
  20. $zip = new Ziparchive ();
  21. if ($zip->open ($destination, $overwrite? Ziparchive::overwrite:ziparchive::create)!== true) {
  22. return false;
  23. }
  24. Add the Files
  25. foreach ($valid _files as $file) {
  26. $zip->addfile ($file, $file);
  27. }
  28. Debug
  29. Echo ' The zip archive contains ', $zip->numfiles, ' files with a status of ', $zip->status;
  30. Close the zip--done!
  31. $zip->close ();
  32. Check to make sure the file exists
  33. return file_exists ($destination);
  34. }
  35. Else
  36. {
  37. return false;
  38. }
  39. }
  40. /***** Application Example ***/
  41. $files =array (' 1.php ', ' mail.php ', ' readme.txt ');
  42. Create_zip ($files, ' Myzipfile.zip ', true);
  43. ?>
Copy Code
  • 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.