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

作者:吕滔 时间:2024-06-05 09:47:49 

本文实例为大家分享了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 != "." &amp;&amp; $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文件
}

标签:PHP,文件下载
0
投稿

猜你喜欢

  • 基于python实现模拟数据结构模型

    2022-11-12 23:44:01
  • 实现 Python 脚本生成命令行

    2022-08-26 01:05:56
  • python实现图像边缘检测

    2022-03-17 15:35:11
  • Python利用wxPython制作股票价格查询工具

    2021-11-25 19:09:52
  • CSS网页设计时关于字体大小的设计

    2008-10-23 13:42:00
  • MYSQL复杂查询练习题以及答案大全(难度适中)

    2024-01-12 14:54:45
  • Python Web后端开发中的增查改删处理

    2022-11-21 06:15:42
  • Golang errgroup 设计及实现原理解析

    2024-05-08 10:53:06
  • mysql表名忽略大小写配置方法详解

    2024-01-24 11:49:19
  • ASP分页函数

    2009-07-06 12:41:00
  • Python视频编辑库MoviePy的使用

    2022-12-22 21:07:07
  • Python基于lxml模块解析html获取页面内所有叶子节点xpath路径功能示例

    2022-11-13 00:12:24
  • 用户体验的另一种认识

    2007-10-25 12:36:00
  • win10系统配置GPU版本Pytorch的详细教程

    2023-07-21 19:30:38
  • Python使用os模块实现更高效地读写文件

    2021-08-11 00:41:39
  • 几个ASP字符串处理函数

    2008-04-23 12:55:00
  • javascript应用:Iframe自适应其加载的内容高度

    2024-02-25 13:46:07
  • python实战教程之OCR文字识别方法汇总

    2021-09-20 05:36:21
  • 实例讲解Python中函数的调用与定义

    2022-10-25 02:47:55
  • 设计评论表单[译]

    2009-03-25 20:41:00
  • asp之家 网络编程 m.aspxhome.com