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

相关文章

探讨Smarty中如何获取数组的长度以及smarty调用php函数的详解

Smarty中如何获取数组的长度 前提假设:分配了一个数组array给Smarty,假设Smarty的分界符为'{' 和'}'。在很多资料上都看到,在Smarty中要求数组的长度时,可以...

使用eAccelerator加密PHP程序

使用 eAccelerator 加密PHP程序 复制代码 代码如下:# /usr/local/bin/encoder 执行后会看到简单的使用说明: 复制代码 代码如下:Usage: en...

关于php支持的协议与封装协议总结(推荐)

关于php支持的协议与封装协议总结(推荐)

前言 当今web程序的开发技术真是百家争鸣,ASP.NET, PHP, JSP,Perl, AJAX 等等。 无论Web技术在未来如何发展,理解Web程序之间通信的基本协议相当重要, 因...

php下foreach提示Warning:Invalid argument supplied for foreach()的解决方法

本文实例讲述了php下foreach()错误提示Warning: Invalid argument supplied for foreach() 的解决方法。分享给大家供大家参考。具体实...

php基于登陆时间判断实现一天多次登录只积分一次功能示例

本文实例讲述了php基于登陆时间判断实现一天多次登录只积分一次功能。分享给大家供大家参考,具体如下: 在网上找了很多的案例,感觉都差不多,有的还比较的繁琐,就自己尝试了一下,如何实现这个...