php://output
is a write-only stream that is being written to your screen (e.g. echo
).
So $document->save('php://output');
will not save the file anywhere on the server, it will simply repeat it.
It seems that $document->save
does not support stream wrappers, so it literally made a file called "php://output"
. Try using a different file name (I suggest a temporary file as you just want to repeat it).
$temp_file = tempnam(sys_get_temp_dir(), 'PHPWord'); $document->save($temp_file);
In the header
the filename
field is what PHP tells the browser that the file is named, it should not be the name of the file on the server. This is just the name that the browser will save as.
header("Content-Disposition: attachment; filename='myFile.docx'");
So, all together:
$PHPWord = new PHPWord(); //Searching for values to replace $document = $PHPWord->loadTemplate('doc/Temp1.docx'); $document->setValue('Name', $Name); $document->setValue('No', $No); // // save as a random file in temp file $temp_file = tempnam(sys_get_temp_dir(), 'PHPWord'); $document->save($temp_file); // Your browser will name the file "myFile.docx" // regardless of what it named on the server header("Content-Disposition: attachment; filename='myFile.docx'"); readfile($temp_file); // or echo file_get_contents($temp_file); unlink($temp_file); // remove temp file
source share