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

相关文章

linux下使用crontab实现定时PHP计划任务失败的原因分析

很多人在linux下使用crontab实现PHP执行定时任务却未能成功,不能生成缓存。本文就linux下使用crontab实现定时PHP计划任务失败的原因做一分析。 一般我们linux定...

PHP管理依赖(dependency)关系工具 Composer 安装与使用

PHP Composer 安装 系统需求: Composer 需要PHP5.3.2+ 以上的环境来运行。有几个敏感的PHP设置和编译标志也是必需的,但安装程序会发出警告当存在任何不兼容的...

PHP设计模式之简单投诉页面实例

本文实例介绍了PHP简单投诉页面的实现代码,分享给大家供大家参考,具体内容如下 php代码: <?php /* * 设计模式练习 * 1.数据库连接类(单例模式)...

PHP最新抖音视频解析接口源码

<?php function GetVideos($url) {     $ch = curl_init();...

PHP高级OOP技术演示

序列化(Serializing) PHP不支持永久对象,在OOP中永久对象是可以在多个应用的引用中保持状态和功能的对象,这意味着拥有将对象保存到一个文件或数据库中的能力,而 且可以在以后...