Home >Backend Development >PHP Tutorial >A simple way to package folders into zip files in php

A simple way to package folders into zip files in php

高洛峰
高洛峰Original
2016-12-20 16:47:541331browse

The example is as follows:

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文件
}

The above is the simple implementation method of php to package folders into zip files brought by the editor. I hope everyone will support the PHP Chinese website~

More php packages folders into For related articles on the simple implementation method of zip files, please pay attention to 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