Home  >  Article  >  Backend Development  >  php zip compression code

php zip compression code

WBOY
WBOYOriginal
2016-07-25 08:43:22957browse
  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

  32. * File Type File Name
    flowers.jpg
    fun.jpg
    uvamsi.docx
    abc.pdf
  33.  
复制代码

php, zip


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn