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_Cooikes不同页面无法传递的解决方法

这是我刚开始设置的cookies 复制代码 代码如下: setcookie("QQ_access_token",$_SESSION['access_token'],time()+3600...

PHP的引用详解

引用是什么 在 PHP 中引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针,替代的是,引用是符号表别名。注意在 PHP 中,变量名和变量内容是不一样的,因此同样的内容可以...

PHP的简易冒泡法代码分享

很基础的东西,感觉代码还不够简洁,希望高手指导修改 复制代码 代码如下: <?php function BubbleSort($str){ for($i=0;$i<count...

PHP实现读取文件夹及批量重命名文件操作示例

PHP实现读取文件夹及批量重命名文件操作示例

本文实例讲述了PHP实现读取文件夹及批量重命名文件操作。分享给大家供大家参考,具体如下: 以读取从网上下载的评书解压后的文件夹为例,并批量重命名 文件夹内容如下,现在使用php读取这个...

PHP利用str_replace防注入的方法

PHP各种过滤字符函数复制代码 代码如下:   <?php    /**    * 安全过滤函数&n...