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

yipeiwu_com4年前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 用sock技术发送邮件的函数

使用sock技术发邮件,无需服务器支持。速度快!!  复制代码 代码如下:function send_mail($to,$subject,$body) &n...

php中socket通信机制实例详解

本文实例讲述了php中socket通信机制及用法。分享给大家供大家参考。具体分析如下: 一、socket是什么 什么是socket 所谓socket通常也称作"套接字",用于描述ip地址...

10个实用的PHP代码片段

关键词高亮 复制代码 代码如下: function highlight($sString, $aWords) { if (!is_array ($aWords) || empty ($a...

jquery不支持toggle()高(新)版本的问题解决

在js代码中引入以下代码,让高版本的jquery兼容toggle事件。代码如下: /** * Replacement for toggle */ jQuery.fn.toggle...

PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】

PHP实现绘制二叉树图形显示功能详解【包括二叉搜索树、平衡树及红黑树】

本文实例讲述了PHP实现绘制二叉树图形显示功能。分享给大家供大家参考,具体如下: 前言: 最近老师布置了一个作业:理解并实现平衡二叉树和红黑树,本来老师是说用C#写的,但是我学的C#基本...