当前位置:Gxlcms > PHP教程 > php简单创建zip压缩文件的方法,_PHP教程

php简单创建zip压缩文件的方法,_PHP教程

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

php简单创建zip压缩文件的方法,


本文实例讲述了php简单创建zip压缩文件的方法。分享给大家供大家参考,具体如下:

  1. /* creates a compressed zip file */
  2. function create_zip($files = array(),$destination = '',$overwrite = false) {
  3. //if the zip file already exists and overwrite is false, return false
  4. if(file_exists($destination) && !$overwrite) { return false; }
  5. //vars
  6. $valid_files = array();
  7. //if files were passed in...
  8. if(is_array($files)) {
  9. //cycle through each file
  10. foreach($files as $file) {
  11. //make sure the file exists
  12. if(file_exists($file)) {
  13. $valid_files[] = $file;
  14. }
  15. }
  16. }
  17. //if we have good files...
  18. if(count($valid_files)) {
  19. //create the archive
  20. $zip = new ZipArchive();
  21. if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
  22. return false;
  23. }
  24. //add the files
  25. foreach($valid_files as $file) {
  26. $zip->addFile($file,$file);
  27. }
  28. //debug
  29. //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
  30. //close the zip -- done!
  31. $zip->close();
  32. //check to make sure the file exists
  33. return file_exists($destination);
  34. }
  35. else
  36. {
  37. return false;
  38. }
  39. }

使用方法:

  1. $files_to_zip = array(
  2. 'preload-images/1.jpg',
  3. 'preload-images/2.jpg',
  4. 'preload-images/5.jpg',
  5. 'kwicks/ringo.gif',
  6. 'rod.jpg',
  7. 'reddit.gif'
  8. );
  9. //if true, good; if false, zip creation failed
  10. $result = create_zip($files_to_zip,'my-archive.zip');

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP操作zip文件及压缩技巧总结》、《php文件操作总结》、《php正则表达式用法总结》、《PHP+ajax技巧与应用小结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125257.htmlTechArticlephp简单创建zip压缩文件的方法, 本文实例讲述了php简单创建zip压缩文件的方法。分享给大家供大家参考,具体如下: /* creates a compressed z...

人气教程排行