thinkphp PHPWord数据导出word固定模板,ZipArchive文件批量压缩打包下载
作者:互联网
public function down($id){
$res = Db::name('usera')->where('id',$id)->find();
$imgs = htmlspecialchars_decode($res['imgs']);
$imgs = json_decode($imgs,true);
//dump($imgs);
//die;
$pingjiabiao = htmlspecialchars_decode($res['pingjiabiao']);
$pingjiabiao = json_decode($pingjiabiao,true);
if (empty($pingjiabiao)) {
$this->error('评价表未提交');
}
$PHPWord = new PhpWord();
//生成评价表
$templateProcessor = $PHPWord->loadTemplate(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'pingjiamb.docx');
foreach ($pingjiabiao as $k => $v) {
$templateProcessor->setValue($k,$v);
}
$templateProcessor->saveAs(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'pingjia_u'.$id.'.docx');
$wenjuan = htmlspecialchars_decode($res['wenjuandiaocha']);
$wenjuan = json_decode($wenjuan,true);
if (empty($pingjiabiao)) {
$this->error('问卷调查未提交');
}
//生成问卷调查
$templateProcessor = $PHPWord->loadTemplate(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'wenjuanmb.docx');
foreach ($wenjuan as $k => $v) {
$templateProcessor->setValue($k,$v);
}
$templateProcessor->saveAs(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'wenjuan_u'.$id.'.docx');
$filename = "user_".$id.".zip";
// 生成文件
$zip = new \ZipArchive ();
// 使用本类,linux需开启zlib,windows需取消php_zip.dll前的注释
if ($zip->open ($filename ,\ZipArchive::OVERWRITE) !== true) {
//OVERWRITE 参数会覆写压缩包的文件 文件必须已经存在
if($zip->open ($filename ,\ZipArchive::CREATE) !== true){
// 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip
exit ( '无法打开文件,或者文件创建失败' );
}
}
//添加到压缩包
$zip->addFile(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'pingjia_u'.$id.'.docx',"评价表.docx");
$zip->addFile(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'wenjuan_u'.$id.'.docx',"问卷调查.docx");
foreach ($imgs as $v) {
if(array_key_exists("up_img",$v)){
$img = str_replace("http://xiedian.xxxxxx.cn","",$v['up_img']);
$zip->addFile(ROOT_PATH . $img,"images/".$v['title'].".jpg");
}
}
$video = str_replace("http://xiedian.xxxxxx.cn","",$res['video']);
$zip->addFile(ROOT_PATH . $video,"video/video.mp4");
// 关闭
$zip->close ();
//下面是输出下载;
header ( "Cache-Control: max-age=0" );
header ( "Content-Description: File Transfer" );
header ( 'Content-disposition: attachment; filename=' . basename ( $filename ) ); // 文件名
header ( "Content-Type: application/zip" ); // zip格式的
header ( "Content-Transfer-Encoding: binary" ); // 告诉浏览器,这是二进制文件
header ( 'Content-Length: ' . filesize ( $filename ) ); // 告诉浏览器,文件大小
@readfile ( $filename );//输出文件;
exit;
}
标签:docx,word,zip,PHPWord,id,PATH,文件批量,ROOT,DS 来源: https://blog.csdn.net/qq_33382313/article/details/88258388