几行代码轻松实现PHP文件打包下载zip

yipeiwu_com7年前PHP代码库

本文实例为大家分享了PHP文件打包下载zip的具体代码,供大家参考,具体内容如下

<?php
//获取文件列表
function list_dir($dir){
   $result = array();
   if (is_dir($dir)){
   $file_dir = scandir($dir);
   foreach($file_dir as $file){
    if ($file == '.' || $file == '..'){
    continue;
    }
    elseif (is_dir($dir.$file)){
    $result = array_merge($result, list_dir($dir.$file.'/'));
    }
    else{
    array_push($result, $dir.$file);
    }
   }
   }
   return $result;
  }

//获取列表 
$datalist=list_dir('../');
$filename = "./bak.zip"; //最终生成的文件名(含路径)  
if(!file_exists($filename)){  
//重新生成文件  
  $zip = new ZipArchive();//使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释  
  if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {  
    exit('无法打开文件,或者文件创建失败');
  }  
  foreach( $datalist as $val){  
    if(file_exists($val)){  
      $zip->addFile( $val, basename($val));//第二个参数是放在压缩包中的文件名称,如果文件可能会有重复,就需要注意一下  
    }  
  }  
  $zip->close();//关闭  
}  
if(!file_exists($filename)){  
  exit("无法找到文件"); //即使创建,仍有可能失败。。。。  
}  
header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header('Content-disposition: attachment; filename='.basename($filename)); //文件名  
header("Content-Type: application/zip"); //zip格式的  
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件  
header('Content-Length: '. filesize($filename)); //告诉浏览器,文件大小  
@readfile($filename);
?>


PHP ZipArchive是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP扩展已经开启,具体开启方法这里就不说了,不同的平台开启PHP扩增的方法网上都有,如有疑问欢迎交流。

这里整理一下利用php zipArchive进行文件的压缩与解压缩的常用的示例供参考。

一、解压缩zip文件

$zip=new ZipArchive;//新建一个ZipArchive的对象
 if($zip->open('test.zip')===TRUE){
 $zip->extractTo('images');//假设解压缩到在当前路径下images文件夹内
 $zip->close();//关闭处理的zip文件
}

二、将文件压缩成zip文件

$zip=new ZipArchive;
if($zip->open('test.zip',ZipArchive::OVERWRITE)===TRUE){
 $zip->addFile('image.txt');//假设加入的文件名是image.txt,在当前路径下
 $zip->close();
}

三、文件追加内容添加到zip文件

$zip=new ZipArchive;
$res=$zip->open('test.zip',ZipArchive::CREATE);
if($res===TRUE){
 $zip->addFromString('test.txt','file content goes here');
 $zip->close();
 echo 'ok';
}else{
 echo 'failed';
}

四、将文件夹打包成zip文件

function addFileToZip($path,$zip){
 $handler=opendir($path); //打开当前文件夹由$path指定。
 while(($filename=readdir($handler))!==false){
 if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..',不要对他们进行操作
  if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归
  addFileToZip($path."/".$filename, $zip);
  }else{ //将文件加入zip对象
  $zip->addFile($path."/".$filename);
  }
 }
 }
 @closedir($path);
}
$zip=new ZipArchive();
if($zip->open('images.zip', ZipArchive::OVERWRITE)=== TRUE){
 addFileToZip('images/', $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
 $zip->close(); //关闭处理的zip文件
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【宜配屋www.yipeiwu.com】。

相关文章

shopex主机报错误请求解决方案(No such file or directory)

一、shopex主机环境 1、windows 2003 R2 2、iis6.0+php5.0以上 3、mysql5.0以上 如果有希望了解php环境搭配的,请查阅: windows200...

php页面消耗内存过大的处理办法

解决办法: 1,修改 php.ini将memory_limit由 8M 改成 16M(或更大),重启apache服务 2,在PHP 文件中 加入 ini_set(”memory_limi...

php提交表单发送邮件的方法

本文实例讲述了php提交表单发送邮件的方法。分享给大家供大家参考。具体如下: 保存下面的html代码到:email.html文件 <html> <head>...

PHP使用curl函数发送Post请求的注意事项

前言 前几天在工作中遇到了一个需求,当用户点击了某个按钮以后,后台发起一个post请求到我这里,我接受传递过来的数据。一开始同事是用公司框架公共封装的http_request()方法进行...

php魔术方法与魔术变量、内置方法与内置变量的深入分析

php内置变量了:DIRECTORY_SEPARATORDIRECTORY_SEPARATOR是一个返回跟操作系统相关的路径分隔符的php内置命令,在windows上返回/,而在linu...