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

相关文章

php数组操作之键名比较与差集、交集赋值的方法

本文实例讲述了php数组操作之键名比较与差集、交集赋值的方法。分享给大家供大家参考。具体方法如下: 该实例主要实现对数组的各种常见操作。如对键名比较计算数组的差集,计算差集,给指定数组中...

PHP访问Google Search API的方法

本文实例讲述了PHP访问Google Search API的方法。分享给大家供大家参考。具体如下: 这段代码段演示了如何从php向AJAX搜索API发送请求。请注意,此示例假定使用 PH...

php实现的树形结构数据存取类实例

本文实例讲述了php实现的树形结构数据存取类。分享给大家供大家参考。 具体实现代码如下: 复制代码 代码如下:<?php /**  * Tanphp fram...

php生成酷炫的四个字符验证码

本文实例为大家分享php生成验证码的实现代码,供大家参考,具体内容如下 <?php $im=imagecreate(200,100);//生成画布 imagecolo...

php文件后缀不强制为.php的实操方法

php文件后缀不强制为.php的实操方法

在大家学习PHP中,认为php文件的后缀一定为.php,其实不然,我们可以把它写成其他的后缀。这样也可以防止其他人的恶意攻击。 php文件后缀不强制为.php 首先,我们先测试一下,P...