Home  >  Article  >  Backend Development  >  How to implement batch compression download in php

How to implement batch compression download in php

王林
王林Original
2021-10-13 17:25:311918browse

php method to implement batch compression downloads: [

How to implement batch compression download in php

The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.

In php, we can use the ZipArchive class to compress and package files in batches for download.

Note:

Before using this class, zlib needs to be enabled in Linux, and the comment in front of php_zip.dll needs to be uncommented in Windows.

The following is a simple example of compressing a file into zip format.

<?php 
$filename=&#39;test.zip&#39;; //最终生成的文件名(含路径) 
if(file_exists($filename)){ 
    unlink($filename); 
} 
//重新生成文件 
$zip=new ZipArchive(); 
if($zip->open($filename,ZIPARCHIVE::CREATE)!==TRUE){ 
    exit(&#39;无法打开文件,或者文件创建失败&#39;); 
} 
$datalist=array(&#39;try.php&#39;,&#39;zip_class.php&#39;); 
foreach($datalist as $val){ 
    if(file_exists($val)){ 
        $zip->addFile($val); 
    } 
} 
$zip->close();//关闭 
if(!file_exists($filename)){ 
    exit(&#39;无法找到文件&#39;); //即使创建,仍有可能失败 
}

Remarks:

//向压缩包中添加文件(addFile的第二参数表示,替换的名称,若不写则按照前面的命名,当然如果是带目录,在压缩包中也会生成一个文件夹)
$zip->addFile("./down.php","testfromfile.php");
$zip->addFile(&#39;img/images-3.jpg&#39;,&#39;new.jpg&#39;);
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";

Recommended learning: php training

The above is the detailed content of How to implement batch compression download in php. 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