当前位置:Gxlcms > PHP教程 > php的zip压缩代码

php的zip压缩代码

时间:2021-07-01 10:21:17 帮助过:11人阅读

  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

人气教程排行