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

高洛峰
Release: 2023-03-03 18:04:01
Original
1330 people have browsed it

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文件
}
Copy after login

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!

Related labels:
zip
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template