php 压缩文件为zip格式的函数代码

php中文网
发布: 2016-07-25 08:59:22
原创
1032人浏览过
  1. /* @creates a compressed zip file 将多个文件压缩成一个zip文件的函数
  2. * @$files 数组类型 实例array("1.jpg","2.jpg");
  3. * @destination 目标文件的路径 如"c:/androidyue.zip"
  4. * @$overwrite 是否为覆盖与目标文件相同的文件
  5. * @site http://bbs.it-home.org
  6. */
  7. function create_zip($files = array(),$destination = '',$overwrite = false) {
  8. //if the zip file already exists and overwrite is false, return false
  9. //如果zip文件已经存在并且设置为不重写返回false
  10. if(file_exists($destination) && !$overwrite) { return false; }
  11. //vars
  12. $valid_files = array();
  13. //if files were passed in...
  14. //获取到真实有效的文件名
  15. if(is_array($files)) {
  16. //cycle through each file
  17. foreach($files as $file) {
  18. //make sure the file exists
  19. if(file_exists($file)) {
  20. $valid_files[] = $file;
  21. }
  22. }
  23. }
  24. //if we have good files...
  25. //如果存在真实有效的文件
  26. if(count($valid_files)) {
  27. //create the archive
  28. $zip = new ZipArchive();
  29. //打开文件 如果文件已经存在则覆盖,如果没有则创建
  30. if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
  31. return false;
  32. }
  33. //add the files
  34. //向压缩文件中添加文件
  35. foreach($valid_files as $file) {
  36. $zip->addFile($file,$file);
  37. }
  38. //debug
  39. //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
  40. //close the zip -- done!
  41. //关闭文件
  42. $zip->close();
  43. //check to make sure the file exists
  44. //检测文件是否存在
  45. return file_exists($destination);
  46. }else{
  47. //如果没有真实有效的文件返回false
  48. return false;
  49. }
  50. }
  51. /****
  52. //测试函数
  53. $files=array('temp.php','test.php');
  54. create_zip($files, 'myzipfile.zip', true);
  55. ****/
  56. ?>
复制代码


PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号