Auto download the file attachment using PHPWord

前端 未结 7 1755
悲哀的现实
悲哀的现实 2020-12-18 22:44

I\'m trying to use PHPWord to generate word documents. And the document can be generated successfully. But there is a problem where my generated word document will be saved

7条回答
  •  [愿得一人]
    2020-12-18 23:27

    $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
    
    $filename = 'MyFile.docx';
    
    $objWriter->save($filename);
    
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.$filename);
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($filename));
    flush();
    readfile($filename);
    unlink($filename); // deletes the temporary file
    exit;
    

提交回复
热议问题