简体   繁体   English

将PHP文件转换为Word doc并在浏览器中打开

[英]Convert PHP file to Word doc and to open it in browser

I like to convert my PHP file to Word document and I used this code 我喜欢将PHP文件转换为Word文档,并使用了以下代码

<?php
    header("Content-type: application/vnd.ms-word");
    header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">
<title>Saves as a Word Doc</title>
</head>
<body>
<h1>Header</h1>
  This text can be seen in word
<ul>
<li>List 1</li>
<li>List 2</li>
</ul>
</body>
</html>

It works perfectly, it prompting me to open/save the document instead of rendering it like a normal webpage. 它运行完美,提示我打开/保存文档,而不是像普通网页一样呈现它。 But I need to open the same page in browser too. 但是我也需要在浏览器中打开同一页面。

Is there any way to open a PHP page in web browser and also to make it save as word doc. 有什么方法可以在网络浏览器中打开PHP页面,也可以将其另存为Word文档。 If possible, give me code which prompting save as Word doc by clicking a button. 如果可能,请给我提供代码,通过单击一个按钮,提示您另存为Word文档。

Pass it a parameter when you wish to download it: 当您希望下载它时,传递一个参数:

<?php
if( $_GET['action'] == 'download' ) {
  header("Content-type: application/vnd.ms-word");
  header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
?>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<? } ?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">
<title>Saves as a Word Doc</title>
</head>
<body>
<h1>Header</h1>
  This text can be seen in word
<ul>
<li>List 1</li>
<li>List 2</li>
</ul>
</body>
</html>

Then have a link that passed file.php?action=download 然后有一个链接通过file.php?action=download

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM