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

yipeiwu_com5年前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判断是否为ajax请求的方法

本文实例讲述了php判断是否为ajax请求的方法。分享给大家供大家参考,具体如下: 先说前端使用 jQuery 时怎么区分: jQuery 发出 ajax 请求时,会在请求头部添加一个名...

php中substr()函数参数说明及用法实例

本文实例讲述了php中substr()函数参数说明及用法。分享给大家供大家参考。具体如下: string substr(string $string ,int $start [, int...

PHP新手NOTICE错误常见解决方法

刚学习PHP,不久,一般就看看手册,和一本叫PHP和mysql web开发的。 最近在整留言板,刚才遇到个问题。 页面中,好多类似 Notice: Use of undefined co...

PHP缓存机制Output Control详解

在php5.2版本的配置中,默认output_buffering为关闭状态,因此运行下面三行代码将会出现一个警告: Warning: Cannot modify header infor...

php 大数据量及海量数据处理算法总结

下面的方法是我对海量数据的处理方法进行了一个一般性的总结,当然这些方法可能并不能完全覆盖所有的问题,但是这样的一些方法也基本可以处理绝大多数遇到的问题。下面的一些问题基本直接来源于公司的...