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获取参数的几种方法总结

php获取参数的几种方法总结 $value = $_POST["value"];//取得post中的       ...

PHP文件操作实例总结

PHP文件操作实例总结

本文实例总结了PHP文件操作。分享给大家供大家参考,具体如下: 操作文件的常用方法: flie_put_contents(url,str); file_get_contents(url...

PHP 5.3和PHP 5.4出现FastCGI Error解决方法

不少童鞋在配置完PHP 5.3或者PHP 5.4网站源码后打开出错,提示 复制代码 代码如下: FastCGI Error The FastCGI Handler was unable...

PHP递归实现层级树状展开

PHP递归实现层级树状展开

本文实例为大家分享了PHP递归实现层级树状展开的主要代码,供大家参考,具体内容如下 效果图: 实现代码: <?php $db = mysql_connect(...

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

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