PHP Zip压缩 在线对文件进行压缩的函数

yipeiwu_com6年前PHP代码库
复制代码 代码如下:

/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return 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) {
$zip->addFile($file,$file);
}
//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
{
return false;
}
}
/***** Example Usage ***/
$files=array('file1.jpg', 'file2.jpg', 'file3.gif');
create_zip($files, 'myzipfile.zip', true);

PHP Zip 文件在线解压缩的函数代码

相关文章

PHP中引用类型和值类型功能与用法示例

本文实例讲述了PHP中引用类型和值类型功能与用法。分享给大家供大家参考,具体如下: PHP中的四种简单类型和复杂类型array都是值类型。同类型间赋值传递的是值,即创建一个副本给新变量。...

php实现的微信分享到朋友圈并记录分享次数功能

本文实例讲述了php实现的微信分享到朋友圈并记录分享次数功能。分享给大家供大家参考,具体如下: 1.引入JS文件 2.通过config接口注入权限验证配置 3.通过ready接口处理成功...

PHP异常处理定义与使用方法分析

本文实例讲述了PHP异常处理定义与使用方法。分享给大家供大家参考,具体如下: <?php //php5提供了基本的异常处理类,可直接使用 ,不需要自己再定义 // cl...

php Rename 更改文件、文件夹名称

php Rename 更改文件、文件夹名称

命令格式为: bool rename ( string oldname, string newname [, resource context] )   下面演示rename的具体应用:...

PHP中Cookie的使用详解(简单易懂)

PHP中Cookie的使用---添加/更新/删除/获取Cookie 及 自动填写该用户的用户名和密码和判断是否第一次登陆 什么是cookie 服务器在客户端保存用户的信息,比如登录名,密...