PHP fragment instantly compress zip file

Source: Internet
Author: User
Tags ziparchive

Zip files can be compressed instantly using the following PHP fragment

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