Search code examples
phpphppresentation

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


How do I modify the following script (Summary.php) so that the served PPTX file name is not "Summary.php" and my served file is not named Summary.php.pptx?

The technique I am using is to render contents of the PowerPoint file into into the browser but I intentionally want to change the filename to at least not contain .php, but at best be a custom name like Presentation.pptx as specified in my php header below.

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');
    }
}

EDIT

Solution

    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');

Solution

  • You should use Content-Disposition response header. E.g.

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

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