[英]how to save .html file by PHP by save as button?
I've created a PHP file which shows the results from a MySQL database as we all create like in 我创建了一个PHP文件,该文件显示了MySQL数据库的结果,就像我们在
echo "<table><tr><td>";
...
echo "</td></tr></table>";
But now I want to make a button at the bottom of the table, something like 'save report' , which saves the created table into HTML format. 但是现在我想在表格底部创建一个按钮,类似于“保存报告” ,它将创建的表格保存为HTML格式。
So how could it be done? 那怎么办呢?
You can use the following scripts: 您可以使用以下脚本:
index.php index.php
In index.php, you have the HTML table. 在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
And then use the file savefile.php to popup your browser's download dialog to save the file. 然后使用文件savefile.php弹出浏览器的下载对话框以保存文件。
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
header('Content-type: text/html');
header('Content-Disposition: attachment; filename="table.html"');
echo $_POST['contents'];
}
?>
I think you want to save reports generated by PHP/MySQL as HTML file? 我认为您想将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);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.