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

相关文章

用php+javascript实现二级级联菜单的制作

 大体思路是这样的:为了不让先前的页面刷新,我用iframe潜入了一个二级子页面,用来读取数据库中的数据,最后把想要的数据传递给父级页面,完成数据的选择和转移。 主要程序代码如...

php环境无法上传文件的解决方法

一、 检查网站目录的权限。 上传目录是否有写入权限。二、 php.ini配置文件 php.ini中影响上传的有以下几处: file_uploads 是否开启 on 必须开启 是否允许HT...

国外比较好的几个的Php开源建站平台小结

博客:WordPress WordPress是使用PHP开发的著名博客平台,免费开源,功能强大,不仅仅用于博客搭建,还可以广泛应用于各类网络信息发布平台。 论坛:phpBB phpBB是...

PHP实现按之字形顺序打印二叉树的方法

本文实例讲述了PHP实现按之字形顺序打印二叉树的方法。分享给大家供大家参考,具体如下: 问题 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序...

PHP改进计算字符串相似度的函数similar_text()、levenshtein()

similar_text()中文汉字版 复制代码 代码如下:      <?php     ...