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中include()与require()的区别说明

require 的使用方法如 require("MyRequireFile.php"); 。这个函数通常放在 PHP 程序的最前面,PHP 程序在执行前,就会先读入 require 所指...

PHP实现的构造sql语句类实例

本文实例讲述了PHP实现的构造sql语句类。分享给大家供大家参考,具体如下: /** * @package Database Class * @author injection (m...

php中current、next与reset函数用法实例

本文实例讲述了php中current、next与reset函数用法。分享给大家供大家参考。 具体代码如下: 复制代码 代码如下:$array=array('step one','step...

PHP中配置IIS7实现基本身份验证的方法

PHP中配置IIS7实现基本身份验证的方法

在PHP运行环境中配置IIS7实现基本身份验证的方法,其实IIS7身份验证的方法有好几种,比如Windows身份验证、摘要式身份验证等,相对来说IIS7基本身份验证是最简单的一种,下面以...

php数组函数序列之array_keys() - 获取数组键名

array_keys() 定义和用法 array_keys() 函数返回包含数组中所有键名的一个新数组。 如果提供了第二个参数,则只返回键值为该值的键名。 如果 strict 参数指定为...