【问题标题】:PHP readfile with PHPPowerpoint issue带有 PHPPowerpoint 问题的 PHP 读取文件
【发布时间】:2014-02-05 13:54:09
【问题描述】:

我正在使用 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是在服务器上创建的,可以打开,没问题。但它不会下载文件。我从这个线程中得到了内容类型:What is a correct mime type for docx, pptx etc?。我也尝试了许多其他类型。当我控制台记录我的响应时,我得到一个奇怪的字符串(很长),像这样开始: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);

谁能帮忙?

【问题讨论】:

    标签: php powerpoint readfile phppowerpoint


    【解决方案1】:

    以下标题应该可以工作;而且直接流式传输到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');
    

    【讨论】:

    • 感谢您的回复。但是,现在它只是停滞不前,我没有得到任何回应。一旦我删除标题('Content-Type ...我突然再次收到响应,但仍然没有下载任何内容。我不需要readfile,flush,ob_clean?
    • 检查实际发送到浏览器的标头,以确保您没有其他任何东西会触发标准 HTML 标头被发送
    • 保存到php://output filestream 应该将文件直接发送到当前的 PHP 输出流,(在 Web SAPI 的情况下)应该直接发送到浏览器
    • 在我的 .php 脚本中,没有其他标题标签。上面的代码是您自己项目中的示例吗?你能下载 pptx 文件吗?
    • 似乎无法接受内容类型。我一添加那行,它就冻结了。
    猜你喜欢
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    相关资源
    最近更新 更多