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 文件在线解压缩的函数代码

相关文章

探讨file_get_contents与curl效率及稳定性的分析

做过好多抓取别家网站内容的产品,习惯了使用方便快捷的file_get_contents函数,但是总是会遇到获取失败的问题,尽管按照手册中的例子设置了超时,可多数时候不会奏效:复制代码 代...

PHP中$this和$that指针使用实例

PHP5中定义了一个特殊的方法名“__clone()”方法,是在对象克隆时自动调用的方法,用“__clone()”方法将建立一个与原对象拥有相同属性和方法的对象,如果想在克隆后改变原对象...

PHP中输出转义JavaScript代码的实现代码

分享一下: 复制代码 代码如下: function jsformat($str) { $str = trim($str); $str = str_replace('\\s\\s', '\...

PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)

本文实例总结了PHP常用字符串操作函数。分享给大家供大家参考,具体如下: /*常用的字符串输出函数 * * echo() 输出字符串 * print() 输出一个或多个字符串 * d...

PHP根据手机号判断运营商(详细介绍附代码)

道理很简单,知道手机号规则 进行正则判断就可以 移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188 联通:130、131...