php://output是一个只写流,写入您的屏幕(如echo)。
因此,
$document->save('php://output');不会将文件保存在服务器上的任何位置,它只会将其回显出来。
似乎,$document->save,不支持流封装,所以它从字面上做了一个名为"php://output"的文件。尝试使用另一个文件名(我建议使用一个临时文件,因为您只想将其回显出来)。
$temp_file = tempnam(sys_get_temp_dir(), 'PHPWord');
$document->save($temp_file);
在header,该filename场就是PHP告诉浏览器文件被命名为,它并不必须是服务器上的文件的名称。这只是浏览器将其保存为的名称。
header("Content-Disposition: attachment; filename='myFile.docx'");
所以,把他们放在一起:
$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's 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