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 dirname(__FILE__) 获取当前文件的绝对路径

PHP 常量 dirname(__file__) __FILE__ :被称为PHP魔术常量 ,返回当前执行PHP脚本的完整路径和文件名,包含一个绝对路径 1)dirname(__FILE...

PHP简单实现循环链表功能示例

PHP简单实现循环链表功能示例

本文实例讲述了PHP简单实现循环链表功能。分享给大家供大家参考,具体如下: 概述: 循环链表是另一种形式的链式存贮结构。它的特点是表中最后一个结点的指针域指向头结点,整个链表形成一个环。...

PHP中判断变量为空的几种方法小结

1. isset功能:判断变量是否被初始化 说明:它并不会判断变量是否为空,并且可以用来判断数组中元素是否被定义过注意:当使用isset来判断数组元素是否被初始化过时,它的效率比arra...

php的日期处理函数及uchome的function_coomon中日期处理函数的研究

复制代码 代码如下: <?php echo time(); echo mktime(11,25,0,9,5,2010);//和time一样的 echo microtime(); e...

使用PHP编写的SVN类

复制代码 代码如下:<?php/** * SVN 外部命令 类 * * @author rubekid * * @todo com...