PHPPresentation - 如何自定义服务文件的文件名(可在浏览器中下载)而不是调用 php 脚本的文件名?

PHPPresentation - How to customize filename of served (downloadable in browser) file to not be the filename of the invoking php script?

如何修改以下脚本 (Summary.php) 以便提供的 PPTX 文件名不是 "Summary.php" 并且我提供的文件不名为 Summary.php.pptx?

我使用的技术是将 PowerPoint 文件的内容渲染到浏览器中,但我有意将文件名更改为至少不包含 .php,但最好是自定义名称,例如Presentation.pptx 如我在下面的 php header 中指定的那样。

Summary.php

require_once 'vendor/phpoffice/phppresentation/vendor/autoload.php';
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\Style\Alignment;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Fill;
use PhpOffice\PhpPresentation\Style\Border;


class Presentation
{
    function generatePresentation()
    {
        header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation; filename=Presentation.pptx");
        $oWriterPPTX = IOFactory::createWriter($this->objPhpPresentation,'PowerPoint2007' );
        $oWriterPPTX->save('php://output');
    }
}

编辑

解决方案

    header("Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation");
    header("Content-Disposition: attachment; filename=Presentation.pptx");
    $oWriterPPTX = IOFactory::createWriter($this->objPhpPresentation,'PowerPoint2007' );
    $oWriterPPTX->save('php://output');

您应该使用 Content-Disposition 响应 header。例如

Content-Disposition: attachment; filename="filename.jpg"

参考:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition