欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php的zip压缩代码

程序员文章站 2024-02-11 09:09:04
...
  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
    php的zip压缩代码 flowers.jpg
    php的zip压缩代码 fun.jpg
    php的zip压缩代码 uvamsi.docx
    php的zip压缩代码 abc.pdf
复制代码

php, zip
相关标签: php的zip压缩代码