PHP 压缩单个或多个文件成ZIP 函数(修复)
来源:互联网 发布:数控圆弧手动编程举例 编辑:程序博客网 时间:2024/04/29 10:14
/* @creates a compressed zip file 将多个文件压缩成一个zip文件的函数 * @$files 数组类型 实例array("1.jpg","2.jpg"); * @destination 目标文件的路径 如"c:/androidyue.zip" * @$overwrite 是否为覆盖与目标文件相同的文件 * @Recorded By Androidyue * @Blog:http://thinkblog.sinaapp.com */ function create_zip($files = array(),$destination = '',$overwrite = false) { //if the zip file already exists and overwrite is false, return false //如果zip文件已经存在并且设置为不重写返回false if(file_exists($destination) && !$overwrite) { return false; } //vars $valid_files = array(); //if files were passed in... //获取到真实有效的文件名 if(is_array($files)) { //cycle through each file foreach($files as $file) { //make sure the file exists if(file_exists($file)) { $valid_files[] = $file; } } } //if we have good files... //如果存在真实有效的文件 if(count($valid_files)) { //create the archive $zip = new ZipArchive(); //打开文件 如果文件已经存在则覆盖,如果没有则创建 if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { return false; } //add the files //向压缩文件中添加文件 foreach($valid_files as $file) { $file_info_arr= pathinfo($file); $filename =$file_info_arr['basename']; $zip->addFile($file,$filename); } //debug //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status; //close the zip -- done! //关闭文件 $zip->close(); //check to make sure the file exists //检测文件是否存在 return file_exists($destination); }else{ //如果没有真实有效的文件返回false return false; } } [php] view plaincopyprint? 原函数中://向压缩文件中添加文件 foreach($valid_files as $file) { $zip->addFile($file,$file); } 此处经过测试存在bug,压缩后的zip中包含了zip所在的目录结构,所以修改成: //向压缩文件中添加文件 foreach($valid_files as $file) { $file_info_arr= pathinfo($file); $filename =$file_info_arr['basename']; $zip->addFile($file,$filename); }
0 0
- PHP 压缩单个或多个文件成ZIP 函数(修复)
- PHP 压缩单个或多个文件成ZIP 函数(修复)
- PHP 压缩单个或多个文件成ZIP 函数(修复)
- PHP 压缩单个或多个文件成ZIP 函数(修复)
- java实现单个或多个文件的压缩、解压缩 支持zip、rar等格式
- java实现单个文件多个文件的zip压缩
- zip压缩多个路径的文件或目录到指定zip
- php上传单个或者多个文件函数
- JAVA 单个或多个下载服务器上带超链接的文件 并压缩后下载
- 把多个文件打包压缩成ZIP文件
- 使用java zip 压缩单个文件
- java 压缩单个文件,多个文件,或者压缩文件夹
- Struts2上传单个或多个文件
- android上传单个或多个文件
- java 单个或多个文件上传
- 上传单个或多个文件
- java zip单文件或多文件 压缩和解压缩
- Java实现zip文件压缩(单个文件、文件夹以及文件和文件夹的组合压缩)
- Java之IO、NIO、Buffer
- 微信营销如何掌握潜在客户
- C++中的枚举类型
- <liunx 学习> mysql 中新建用户和用户权限
- android Volley JSON请求
- PHP 压缩单个或多个文件成ZIP 函数(修复)
- AMD模块定义规范
- Linux/Android——Input系统之frameworks层InputManagerService (六)
- 如何调试opencv源函数
- openSUSE 中 Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6_3.3.x86_64
- Celery
- 上海首套房 首付 贷款 契税
- DISTINCT 多个字段
- OS开发如何提高