2012-11-22 235 views
-1

我创建了一个PHP文件,显示从MySQL数据库的结果我们都创造像如何将保存为按钮的.html文件保存为PHP?

echo "<table><tr><td>"; 
... 
echo "</td></tr></table>"; 

但现在我想打在桌子底部的按钮,像“保存报告',它将创建的表格保存为HTML格式。

那么它是如何做到的?

+0

'CTRL + S'我觉得呢? – EaterOfCode

+0

这没什么意义..到目前为止你做了什么?你说过你创建了一个PHP脚本来制作一个HTML表格,但接着询问如何将PHP表格保存为HTML格式?世界上没有这样的PHP表“ – George

+0

看到这个http://php.net/manual/en/function.fwrite.php 谷歌是你的朋友(Y) – Ace

回答

2

您可以使用下面的脚本:

的index.php
在index.php文件,你有HTML表格。

<?php 

$contents = "<table><tr><td>A</td><td>B</td></tr><tr><td>One</td><td>Two</td></tr><tr><td>Three</td><td>Four</td></tr></table>"; // Put here the source code of your table. 

?> 
<html> 
    <head> 
     <title>Save the file!</title> 
    </head> 
    <body> 

     <?php echo $contents; ?> 

     <form action="savefile.php" method="post"> 
      <input type="hidden" name="contents" value="<?php echo htmlspecialchars($contents); ?>"> 
      <input type="submit" value="Save file" /> 
     </form> 
    </body> 
</html> 

savefile.php
然后使用该文件savefile.php弹出浏览器的下载对话框,保存文件。

<?php 

if ($_SERVER['REQUEST_METHOD'] == "POST") { 
    header('Content-type: text/html'); 
    header('Content-Disposition: attachment; filename="table.html"'); 

    echo $_POST['contents']; 
} 

?> 
1

我想你想保存由PHP/MySQL生成的报告为HTML文件?

<?php 

// Open file for writing 
$fileHandle = fopen("/DestinationPath/DestinationFile.html", "w"); 

// Dump File 
$head = "<html><head><title>my reports</title></head><body>\n"; 
fwrite($fileHandle, $head); 
$sql = mysql_query("Your sql query here"); 
while ($result = mysql_fetch_assoc($sql)) { 
    $line = $result['yourmysqlfield']."\n"; 
    fwrite($fileHandle, $line); 

} 
$foot = "</body></html>\n"; 
fwrite($fileHandle, $foot); 

// Close File 
close($fileHandle); 

?>