使用PHP的ZipArchive类实现多个文件的zip压缩包打包下载
<?php
//新建一个ZipArchive的对象
$zip = new ZipArchive();
//设置.zip下载后的文件名
$zname = time().".zip";
//开始操作.zip压缩包
if($zip->open($zname, ZipArchive::CREATE)===TRUE){
//向.zip压缩包里添加文件
$result = $zip->addFile("aaa.doc");
$res = $zip->addFile("bbb.doc");
//文件添加完,关闭ZipArchive的对象
$zip->close();
//清空(擦除)缓冲区并关闭输出缓冲
ob_end_clean();
//下载建好的.zip压缩包
header("Content-Type: application/force-download");//告诉浏览器强制下载
header("Content-Transfer-Encoding: binary");//声明一个下载的文件
header("Content-Type: application/zip");//设置文件内容类型为zip
header("Content-Disposition: attachment; filename=".$zname);//声明文件名
header("Content-Length: ".filesize($zname));//声明文件大小
error_reporting(0);
//将欲下载的zip文件写入到输出缓冲
readfile($zname);
//将缓冲区的内容立即发送到浏览器,输出
flush();
exit;
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
