建站软件 优化软件 编程软件 网页辅助 站群程序 网站程序 图像处理 资源教程 字体下载 推荐软件


您的位置:首页 > PHP学习 > php压缩zip文件

php压缩zip文件

时间:2015-03-03 17:09:31  来源:免费模板网 作者:风雪 阅读次数 tagszip

既然文件可以解压,那么文件怎么压缩呢?

PHP在线对文件进行Zip 压缩函数代码,用于使用PHP在线创建ZIP压缩文件。

代码如下:

/* 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); 
 

本文地址:https://www.freemoban.com/php/2015/0303/783.html

猜你喜欢
栏目推荐
模板推荐

Copyright:www.freemoban.com 免费模板网 All Rights Reserved 网站备案:辽ICP备19014872号-2   辽公网安备 21010602000376号  辽公网安备:42900402000182号

免责声明:本站部分资源来自互联网收集,版权归原创者所有,如果侵犯了你的权益,我们会及时删除侵权内容,联系QQ:1615187561 谢谢合作!