首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHPWord自动下载文件附件

问使用PHPWord自动下载文件附件
EN

Stack Overflow用户
提问于 2012-07-14 02:15:36
回答 6查看 36.1K关注 0票数 15

我正在尝试使用PHPWord生成word文档。可以成功生成单据。但是有一个问题,我生成的word文档将保存在服务器上。我怎样才能让它可以直接下载?

示例:

代码语言:javascript
复制
$PHPWord = new PHPWord();
//Searching for values to replace
$document = $PHPWord->loadTemplate('doc/Temp1.docx');
$document->setValue('Name', $Name);
$document->setValue('No', $No);
$document->save('php://output'); //it auto save into my 'doc' directory.

如何链接到header下载它,如下所示:

代码语言:javascript
复制
header("Content-Disposition: attachment; filename='php://output'"); //not sure how to link this filename to the php://output..

敬请指教。

EN

回答 6

Stack Overflow用户

发布于 2013-05-22 17:42:39

代码语言:javascript
复制
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');

$filename = 'MyFile.docx';

$objWriter->save($filename);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
flush();
readfile($filename);
unlink($filename); // deletes the temporary file
exit;
票数 10
EN

Stack Overflow用户

发布于 2017-10-19 02:32:35

这是我的工作:

代码语言:javascript
复制
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007', $download = true);

header("Content-Disposition: attachment; filename='File.docx'");

$objWriter->save("php://output");
票数 5
EN

Stack Overflow用户

发布于 2017-08-25 12:51:06

现在是0.13.0 https://github.com/PHPOffice/PHPWord版本

代码语言:javascript
复制
<?
require_once "../include/PHPWord-develop/bootstrap.php";

$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('template.docx');

$templateProcessor->setValue('var01', 'Sun');
$templateProcessor->setValue('var02', 'Mercury');

//#####################################################
// Save File
//#####################################################
//#####################################################
header("Content-Disposition: attachment; filename='output01.docx'");
  $templateProcessor->saveAs('php://output');
//#####################################################
//#####################################################
?>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11476058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档