Question

Can you tell me what is wrong with my headers? My download is not effective. I use PHPWord library but I don't think it's a problem

<?php

    require_once 'PHPWord.php';
    $PHPWord = new PHPWord();

    $section = $PHPWord->createSection();
    $wordText = utf8_encode($_REQUEST['TEXT']);

    $section->addText($wordText);

    $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
    //$objWriter->save('helloWorld.docx');
    $path = 'tmp/kikou2.docx';
    $objWriter->save($path);
    //download code
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' .$path);
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Content-Length: ' . filesize($objWriter));
    readfile($objWriter);
    unlink($objWriter); // deletes the temporary file
    exit;


?>

Thanks

Was it helpful?

Solution

Recommends headers for an OfficeOpenXML .docx file

// Redirect output to a client’s web browser (.docx)
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="kikou2.docx"');
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

paying particular attention to the Content-Type heading

OTHER TIPS

header('Content-Description: File Transfer');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment; filename="'.basename($objWriter).'"'); //<<< Note the " " surrounding the file name
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($objWriter));

Add two headers:

header("Content-Type: application/force-download");
header("Content-Type: application/download");
            $document->save($url.'temp/Form Letters1.docx');
            $path = $url.'temp/Form Letters1.docx'; 
            header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
            header('Content-Disposition: attachment;filename="Form Letters1.docx"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($path));
            ob_clean();
            flush();
            readfile($path); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top