Php zip compression code

Source: Internet
Author: User
Tags create zip zip extension ziparchive
Php zip compression code

  1. $ Error = ""; // error holder
  2. If (isset ($ _ POST ['createpdf ']) {
  3. $ Post =$ _ POST;
  4. $ File_folder = "files/"; // folder to load files
  5. If (extension_loaded ('Zip') {// Checking zip extension is available
  6. If (isset ($ post ['Files']) and count ($ post ['Files'])> 0) {// Checking files are selected
  7. $ Zip = new ZipArchive (); // Load zip library
  8. $ Zip_name = time (). ". zip"; // Zip name
  9. If ($ zip-> open ($ zip_name, ZIPARCHIVE: CREATE )! = TRUE) {// Opening zip file to load files
  10. $ Error. = "* Sorry ZIP creation failed at this time
    ";
  11. }
  12. Foreach ($ post ['Files'] as $ file ){
  13. $ Zip-> addFile ($ file_folder. $ file); // Adding files into zip
  14. }
  15. $ Zip-> close ();
  16. If (file_exists ($ zip_name )){
  17. // Push to download the zip
  18. Header ('content-type: application/zip ');
  19. Header ('content-Disposition: attachment; filename = "'. $ zip_name .'"');
  20. Readfile ($ zip_name );
  21. // Remove zip file is exists in temp path
  22. Unlink ($ zip_name );
  23. }
  24. } Else
  25. $ Error. = "* Please select file to zip
    ";
  26. } Else
  27. $ Error. = "* You dont have ZIP extension
    ";
  28. }
  29. ?>
  30. Download As Zip
  31. Create Zip


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.