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:311773browse

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.

open($filename,ZIPARCHIVE::CREATE)!==TRUE){ 
    exit('无法打开文件,或者文件创建失败'); 
} 
$datalist=array('try.php','zip_class.php'); 
foreach($datalist as $val){ 
    if(file_exists($val)){ 
        $zip->addFile($val); 
    } 
} 
$zip->close();//关闭 
if(!file_exists($filename)){ 
    exit('无法找到文件'); //即使创建,仍有可能失败 
}

Remarks:

//向压缩包中添加文件(addFile的第二参数表示,替换的名称,若不写则按照前面的命名,当然如果是带目录,在压缩包中也会生成一个文件夹)
$zip->addFile("./down.php","testfromfile.php");
$zip->addFile('img/images-3.jpg','new.jpg');
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