php实现网站文件批量压缩下载功能

yipeiwu_com5年前PHP代码库

利用php实现将文件批量压缩打包下载,这个过程中将使用到 ZipArchive 这个类,注意使用该类之前,linux需开启zlib,windows需取消php_zip.dll前的注释。下面直接给出一个简单的将文件压缩为 zip 格式的示例。具体用法请查询php相关文档。

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

以上就是php实现将文件批量压缩打包下载的全部内容,我们也还可以利用php调用linux系统的shell脚本来实现这个功能,这是一个思路,希望大家可以研究研究。

相关文章

PHP中error_reporting()函数的用法(修改PHP屏蔽错误)

今天学习CI框架过程中遇到个问题: A PHP Error was encountered Severity: Notice Message: Undefined variable: u...

PhpDocumentor 2安装以及生成API文档的方法

官网地址:http://www.phpdoc.org/项目地址:https://github.com/phpDocumentor/phpDocumentor2 phpDocumentor...

linux下为php添加iconv模块的方法

./configure --with-mysql=/backup/mysql --with-freetype-dir --with-jpeg-dir --with-png-dir --w...

Linux下快速搭建php开发环境

Linux下快速搭建php开发环境

一、Linux下快速搭建php开发环境 1.安装XAMPP for Linux XAMPP(Apache+MySQL+PHP+PERL)是一个功能强大的建站集成软件包,使用XAMPP可快...

php 解压rar文件及zip文件的方法

对于zip文件网上的例子很多,rar文件解压php没有直接支持,可以用pecl到http://pecl.php.net/package/rar 下载对应版本的 非线程安全的dll然后扔到...