PHP compresses files into zip files

Source: Internet
Author: User
PHP ziparchive is a PHP extension class, you can easily achieve the zip file compression and decompression, before using the first to ensure that the PHP ZIP extension is open.
  1. /* Description: A function that compresses multiple files into a single zip file
  2. * @param $files Array type
  3. * @param the path to the destination target file
  4. * @param $overwrite whether to overwrite the same file as the target file
  5. */
  6. function Create_zip ($files = Array (), $destination = ", $overwrite = False) {
  7. Returns False if the zip file already exists and is set to No Override
  8. if (file_exists ($destination) &&! $overwrite) {return false;}
  9. $valid _files = Array ();
  10. Get to a real valid file name
  11. if (Is_array ($files)) {
  12. foreach ($files as $file) {
  13. if (file_exists ($file)) {
  14. $valid _files[] = $file;
  15. }
  16. }
  17. }
  18. If there is a real valid file
  19. if (count ($valid _files)) {
  20. $zip = new Ziparchive ();
  21. Open file Overwrite if file already exists, if not, create
  22. if ($zip->open ($destination, $overwrite? Ziparchive::overwrite:ziparchive::create)!== true) {return false;}
  23. To add a file to a compressed file
  24. foreach ($valid _files as $file) {
  25. $zip->addfile ($file, $file);
  26. }
  27. Close File
  28. $zip->close ();
  29. Detects if a file exists
  30. return file_exists ($destination);
  31. }else{
  32. return false;
  33. }
  34. }
  35. $files = Array (' tg.php ');
  36. Create_zip ($files, ' Tg.zip ', true);
  37. ?>
Copy Code
File compression, 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.