Home  >  Article  >  Backend Development  >  Implementation of PHP single file compression function

Implementation of PHP single file compression function

不言
不言Original
2018-04-20 11:38:07992browse

The content of this article is about the implementation of the PHP single file compression function. The correct writing method of recursion has a certain reference value. Now I share it with everyone. Friends in need can refer to it

function zip_file(string $filename){
  if(!is_file($filename)){
    return false;
  }
  $zip=new ZipArchive();
  $zipName=basename($filename).'.zip';
  //打开指定压缩包,不存在则创建,存在则覆盖
  if($zip->open($zipName,ZipArchive::CREATE|ZipArchive::OVERWRITE)){
    //将文件添加到压缩包中
    if($zip->addFile($filename)){
      @unlink($filename);
    }
    $zip->close();
    return true;
  }else{
    return false;
  }
}

Related recommendations:

php function to implement decompression function

The above is the detailed content of Implementation of PHP single file compression function. 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