Home >Backend Development >PHP Tutorial >PHP implements batch compression download function of website files_php skills
Use PHP to compress and package files in batches for download. The ZipArchive class will be used in this process. Note that before using this class, zlib needs to be enabled in Linux and the comment before php_zip.dll needs to be uncommented in Windows. A simple example of compressing a file into zip format is given directly below. Please check the relevant PHP documentation for specific usage.
<?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('无法找到文件'); //即使创建,仍有可能失败 }
The above is the entire content of PHP to realize batch compression, packaging and downloading of files. We can also use PHP to call the shell script of the Linux system to achieve this function. This is an idea, and I hope you can study it.