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自动判断字符集并转码的详解

原理很简单,因为gb2312/gbk是中文两字节,这两个字节是有取值范围的,而utf-8中汉字是三字节,同样每个字节也有取值范围。而英文不 管在何种编码情况下,都是小于128,只占用一个...

PHP中使用xmlreader读取xml数据示例

有一个XML文件,内容如下: 复制代码 代码如下: <?xml version="1.0"?>  <shows>  &nb...

PHP获取汉字笔画数功能【测试可用】

本文实例讲述了PHP获取汉字笔画数功能。分享给大家供大家参考,具体如下: 无意中看到这么个东西,用PHP得到汉字的笔画数。以类的方式实现,有那么点意思,先留下了。 <?...

PHP扩展模块memcached长连接使用方法分析

      网上广泛流传着一篇文章,讲述php的两个扩展模块memcache和memcached的区别,其中特意强调了memcached...

php日志函数error_log用法实例分析

本文实例讲述了php日志函数error_log用法。分享给大家供大家参考,具体如下: php内置打印log日志的函数,这个对php程序调试非常高效 1.配置 编辑php.ini文件...