如何从PHP表单输入创建word文档并将其保存在文件中

时间:2015-10-29 13:19:56

标签: php

我有一个表单页面

的index.php

<form name="proposal_form" action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
  <table cellpadding="0" cellspacing="0" border="0" width="100%"> 
          <tr> 
            <td width="23%" class="body"> Name</td> 
            <td width="3%" class="body">:</td> 
            <td width="74%"><input type="text" name="strname" class="textfield"></td> 
        </tr> 


      </table>    
  <input type="submit" name="submit_docs" value="Export as MS Word" class="input-button" />
</form>

<?php


  if(isset($_POST['submit_docs'])){
 $doc_body ='
 <table cellpadding="0" cellspacing="0" border="0" width="100%"> 
          <tr> 
            <td width="23%" class="body"> Name</td> 
            <td width="3%" class="body">:</td> 
            <td width="74%">'.$_POST["strname"].'</td> 
        </tr> 

      </table>    
 ';}

  header("Content-Type: application/vnd.msword");
  header("Expires: 0");//no-cache
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");//no-cache
 header("content-disposition: attachment;filename=sampleword.doc"); 
 //store in local file
file_put_contents( "/files/sample.doc",$doc_body);
 ?>

我正在尝试在提交表单时创建一个word文件但它没有显示在文件夹中。

知道我在哪里做错了

1 个答案:

答案 0 :(得分:0)

在PHP中尝试将其另存为Word文档。只需使用您的值而不是我输入的示例即可。

<?php
$value1 = $_POST['Name'];
$value2 = $_POST['Phone'];
$value3 = $_POST['State1'];
$value4 = $_POST['MemberID'];
$value5 = $_POST['TaxID'];
$value6 = $_POST['UserID'];
$value7 = $_POST['Facility'];
$value8 = $_POST['Email'];
$value9 = $_POST['AdditionalNotes'];


header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=PSSD.doc");

echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
echo "<body>";
echo "<b><i>Name:</i> ".$value1."</b> \t \t \n <br>";
echo "<b><i>Phone#:</i> ".$value2."</b> \t \t \n <br>";
echo "<b><i>State:</i> ".$value3."</b> \t \t \n <br>";
echo "<b><i>Member ID:</i>".$value4."</b> \t \t \n <br>";
echo "<b><i>Tax ID: </i>".$value5."</b> \t \t \n <br>";
echo "<b><i>User ID:</i> ".$value6."</b> \t \t \n <br>";
echo "<b><i>Facility: </i>".$value7."</b> \t \t \n <br>";`enter code here`
echo "<b><i>Email: </i>".$value8."</b> \t \t \n <br>";
echo "<b><i>Additional Notes: </i>".$value9."</b> \t \t \n <br>";

echo "</body>";
echo "</html>";
?>