首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有PHPPowerpoint问题的PHP readfile

带有PHPPowerpoint问题的PHP readfile
EN

Stack Overflow用户
提问于 2014-02-05 21:54:10
回答 1查看 1.3K关注 0票数 0

我正在使用PHPPowerpoint创建一个带有一些图表的pptx文件,并且可以将其存储在与PHP脚本相同的文件夹中。PHPPowerpoint本身就是这么做的。

我想在创建pptx文件后下载它,到目前为止,我已经尝试了所有我能在web上找到的选项。这就是我尝试这样做的方式。

代码语言:javascript
复制
$file = str_replace('generate_report.php', 'export_download.pptx', __FILE__);
header('Content-Description: File Transfer');
header('Content-disposition: attachment; filename="' . $file . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation');
header('Expires: 0');
header('Cache-Control: ');
header('Pragma: ');
flush();
ob_clean();
readfile($file);

当我执行脚本时,没有下载任何东西。我的pptx是在服务器上创建的,我可以打开它,没有问题。但它不会下载文件。我从这个线程获得了内容类型:What is a correct mime type for docx, pptx etc?。我还尝试了许多其他类型。当我通过控制台记录我的响应时,我得到一个奇怪的字符串(非常长),开头是这样的: PKCTDD����Content_Types.xml͗�n�0E�|E�-J��*�X�����+���m���wBhE

我也尝试了一下:

代码语言:javascript
复制
$handle = fopen($file, 'rb');
$buffer = '';
while (!feof($handle)) {
$buffer = fread($handle, 4096);
echo $buffer;
ob_flush();
flush();
}
fclose($handle);

有人能帮上忙吗?

EN

回答 1

Stack Overflow用户

发布于 2014-02-05 22:01:37

下面的头文件应该可以工作;而且直接流式传输到php://output也比保存到磁盘文件,然后将磁盘文件假脱机到浏览器更好

代码语言:javascript
复制
// Redirect output to a client’s web browser
header('Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation');
header('Content-Disposition: attachment;filename="' . $file . '"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPPowerPoint_IOFactory::createWriter($objPHPPowerPoint, 'PowerPoint2007');
$objWriter->save('php://output');
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21579287

复制
相关文章

相似问题

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