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实例代码中的一些坏代码

做PHP开发已经有快一年的时间了,在这一年的时间中,学习了很多生产环境中的技巧,学习了很多东西,期间也阅读了一些优秀的源码和关于代码的书,对写代码这一块有了一定的思考,也看过很多别人写的...

PHP 中魔术常量的实例详解

PHP 中魔术常量的实例详解 本文介绍下,php编程中的魔术常量,掌握并灵活应用这些方法与常量,对于提高php的编程水平,有很大的帮助。有需要的朋友参考学习下。 魔术常量: nam...

PHP5.6新增加的可变函数参数用法分析

本文实例讲述了PHP5.6新增加的可变函数参数用法。分享给大家供大家参考,具体如下: 今天无事,看了下PHP手册。发现PHP版本更新增加不少东西。下面就说说其中的PHP5.6更新中新增加...

如何在PHP中使用正则表达式进行查找替换

1. preg_match — 执行一个正则表达式匹配int preg_match ( string $pattern , string $subject [, array &$matc...

PHP获取当前日期及本周一是几月几号的方法

本文实例讲述了PHP获取当前日期及本周一是几月几号的方法。分享给大家供大家参考,具体如下: <?php header("content-type:text/html...