Home  >  Article  >  Backend Development  >  A few lines of code to easily package and download PHP files into zip files

A few lines of code to easily package and download PHP files into zip files

不言
不言Original
2018-04-26 15:12:442844browse

This article mainly introduces in detail how to use PHP's own zlib function to easily package and download PHP files into zip. It has certain reference value. Interested friends can refer to the examples in this article.

I have shared the specific code for PHP file packaging and downloading zip for your reference. The specific content is as follows


##

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

//获取列表 
$datalist=list_dir(&#39;../&#39;);
$filename = "./bak.zip"; //最终生成的文件名(含路径)  
if(!file_exists($filename)){  
//重新生成文件  
  $zip = new ZipArchive();//使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释  
  if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {  
    exit(&#39;无法打开文件,或者文件创建失败&#39;);
  }  
  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(&#39;Content-disposition: attachment; filename=&#39;.basename($filename)); //文件名  
header("Content-Type: application/zip"); //zip格式的  
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件  
header(&#39;Content-Length: &#39;. filesize($filename)); //告诉浏览器,文件大小  
@readfile($filename);
?>

PHP ZipArchive It is an extension class that comes with PHP. It can easily compress and decompress ZIP files. Before using it, you must first make sure that the PHP ZIP extension has been turned on. The specific turning method will not be mentioned here. The methods of turning on PHP expansion for different platforms are available online. , if you have any questions, please feel free to communicate.

Here are some common examples of using php zipArchive to compress and decompress files for reference.

1. Unzip the zip file

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

2. Compress the file into a zip file

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

3. File append content is added to the zip file

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

4. Pack the folder into a zip file

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

Related recommendations:


php code example for packaging and downloading files

The above is the detailed content of A few lines of code to easily package and download PHP files into zip files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn