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

相关文章

使用XDebug调试及单元测试覆盖率分析

使用XDebug调试及单元测试覆盖率分析

今天我就就自己对XDebug使用的一些体验做一小段分享。XDebug也是因为需要是用来生成覆盖率分析文件才安装的,刚接触不久,平时用的也不是很频繁,但是这个的确是一个好工具,如果想要依赖...

php判断目录存在的简单方法

PHP判断文件或目录是否存在 file_exists:判断文件是否存在 $file = "check.txt"; if(file_exists($file)) { echo...

php算法实例分享

只打印0   具体个数由输入的参数n决定   如n=5就打印00000 <?php $n = $_GET['n']; for ($i=0; $i < $n...

PHP数组相关函数汇总

本文总结了PHP数组相关的函数。分享给大家供大家参考。具体如下: 这里包括函数名和用法说明,没有详细的代码范例。感兴趣的朋友可以查阅本站相关的函数用法。 数组的相关处理函数 curren...

Linux下快速搭建php开发环境

Linux下快速搭建php开发环境

一、Linux下快速搭建php开发环境 1.安装XAMPP for Linux XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的建站集成软件包,使用XAMPP可快...