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

相关文章

curl 出现错误的调试方法(必看)

实例如下: private function httpGet($url) { $curl = curl_init(); curl_setopt($curl,...

url decode problem 解决方法

试验了一下python的urllib库以及js 的 encodeURIComponent 均不会替换。空格encode也是替换成了 '%20' 。python提供了urllib.quot...

PHP基于swoole多进程操作示例

PHP基于swoole多进程操作示例

本文实例讲述了PHP基于swoole多进程操作。分享给大家供大家参考,具体如下: 多个任务同时执行 将顺序执行的任务,转化为并行执行(任务在逻辑上可以并行执行) 比如,我们要对已知的用户...

PHP变量的定义、可变变量、变量引用、销毁方法

复制代码 代码如下:<?php$long="big_long_variable_name";$$long="PHP";     /...

php中define用法实例

本文实例讲述了php中define用法。分享给大家供大家参考。具体如下: <?php /* * define * */ /*第3个参数: * false --...