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后台进程: $command = " php script.php ";...

php的memcached客户端memcached

memcache的官方主页:http://pecl.php.net/package/memcachememcached的官方主页:http://pecl.php.net/package/...

PHP+Oracle本地开发环境搭建方法详解

安装instant client 首先,是从https://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html下载...

PHP连接MongoDB示例代码

复制代码 代码如下: <?php //这里采用默认连接本机的27017端口,当然你也可以连接远程主机如192.168.0.4:27017,如果端口是27017,端口可以省略 $m...

php关闭warning问题的解决方法

error_reporting 设定错误讯息回报的等级 2047我记得应该是E_ALL。 php.ini 文件中有许多配置设置。您应当已经设置好自己的php.ini 文件并把它放在合适的...