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

相关文章

示例详解Laravel的注册重构

1. 首先确定用户注册的路由 我们在安装好laravel的时候默认生成的注册是用邮箱进行注册的,并且有些选项不需要,有些还需要加一些表单选项 我们注册的话,并不是可以随便注册的,只有一...

PHP常见数学函数及BC高精度数学函数用法示例

本文实例讲述了PHP常见数学函数及BC高精度数学函数用法。分享给大家供大家参考,具体如下: 1. bcadd 任意精度数的相加 2. bcsub 任意精度数的减法 3. bcmul 乘法...

利用PHP自动生成印有用户信息的名片

利用PHP自动生成印有用户信息的名片

前言 无论是自己要在精心P过的自拍上添加个性文字,或者是摄影爱好者要在拍摄的作品里添加水印,亦或是在网页或者移动应用中实时生成文字和图片的组合,我们都需要找到一个合适且靠谱的方法来将图片...

按上下级层次关系输出内容的PHP代码

复制代码 代码如下:function getSubComments($parent = 0, $level = 0) { $db = &JFactory::getDBO(); $sql...

PHP简单判断手机设备的方法

本文实例讲述了PHP简单判断手机设备的方法。分享给大家供大家参考,具体如下: 现在移动互联网越来越发到,很多的网站都普及了手机端浏览,为了更好的让网页在手机端显示,我们都选择了使用CSS...