0

我正在使用 PHPPowerpoint 创建一个带有一些图表的 pptx 文件,并且将其存储在与 PHP 脚本相同的文件夹中没有问题。PHPPowerpoint 自己做的。

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

$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是在服务器上创建的,可以打开,没问题。但它不会下载文件。我从这个线程得到了内容类型:什么是 docx、pptx 等的正确 mime 类型?. 我也尝试了许多其他类型。当我控制台记录我的响应时,我得到一个奇怪的字符串(很长),像这样开始:PKCTDD����[Content_Types].xml͗�n�0E�|E�-J��*�X����� +��������wBhE

也试过这个:

$handle = fopen($file, 'rb');
$buffer = '';
while (!feof($handle)) {
$buffer = fread($handle, 4096);
echo $buffer;
ob_flush();
flush();
}
fclose($handle);

有谁能帮忙吗?

4

1 回答 1

2

以下标题应该可以工作;它也更好地直接流式传输php://output而不是保存到磁盘文件然后将该磁盘文件假脱机到浏览器

// 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');
于 2014-02-05T14:01:36.817 回答