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 on the server. How can I make it available to download straight away?
Sample:
$PHPWord = new PHPWord();
//Searching for values to replace
$document = $PHPWord->loadTemplate('doc/Temp1.docx');
$document->setValue('Name', $Name);
$document->setValue('No', $No);
$document->save('php://output'); //it auto save into my 'doc' directory.
How can i link to the header to download it as follows:
header("Content-Disposition: attachment; filename='php://output'"); //not sure how to link this filename to the php://output..
Kindly advise.
php://outputis a write-only stream, that writes to your screen (likeecho).So,$document->save('php://output');will not save the file anywhere on the server, it will just echo it out.Seems,
$document->save, doesn’t support stream wrappers, so it literally made a file called"php://output". Try using another file name (I suggest a temp file, as you just want to echo it out).In the
header, thefilenamefield is what PHP tells the browser the file is named, it doesn’t have to be a name of a file on the server. It’s just the name the browser will save it as.So, putting it all together: