Home > Article > Backend Development > How to implement batch compression download in php
php method to implement batch compression downloads: [
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='test.zip'; //最终生成的文件名(含路径) if(file_exists($filename)){ unlink($filename); } //重新生成文件 $zip=new ZipArchive(); if($zip->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!