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 h...

php连接微软MSSQL(sql server)完全攻略

php连接微软MSSQL(sql server)完全攻略

在研究ezSQL的时候就看到了mssql_connect()等一些php提供的连接MSSQL的函数,本以为php这个开源的风靡世界的编程语言对连接微软的数据应该是不在话下的,但是到真正执...

php截取utf-8中文字符串乱码的解决方法

复制代码 代码如下:function utf8_substr($str,$len) {   for($i=0;$i<$len;$i++)   {     $temp_str=sub...

PHP中使用数组指针函数操作数组示例

数组的内部指针是数组内部的组织机制,指向一个数组中的某个元素。默认是指向数组中第一个元素通过移动或改变指针的位置,可以访问数组中的任意元素。对于数组指针的控制PHP提供了以下几个内建函数...

php批量添加数据与批量更新数据的实现方法

本文实例讲述了php批量添加数据与批量更新数据的实现方法。分享给大家供大家参考。具体分析如下: php如果要批量保存数据我们只要使用sql的insert into语句就可能实现数据批量保...