Home > Backend Development > PHP Tutorial > PHP implements batch compression download function of website files_php skills

PHP implements batch compression download function of website files_php skills

WBOY
Release: 2016-05-16 20:05:38
Original
1121 people have browsed it

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.

<&#63;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('无法找到文件'); //即使创建,仍有可能失败 
} 
Copy after login

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.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template