【问题标题】:How to create an HTML document with PHP?如何使用 PHP 创建 HTML 文档?
【发布时间】:2014-03-26 04:57:38
【问题描述】:

是的,我想创建一个带有某些表单的 .html 或 .php(随便)文档。

例子:

用户打开一个页面(首选:php)并看到 1 个用于网站标题的表单和 1 个按钮。好的,他将您偏好的网站标题单击按钮。此页面发送表单中包含的信息,并创建一个带有用户在表单中输入的标题的 html/php 文档。

这不是要在 html 中用“echo”显示一些变量,而是要真正创建一些新文档。

有人吗?

【问题讨论】:

标签: php html


【解决方案1】:

这应该做你想做的:

$title = $_POST['title'];
$html = '<html>
    <head>
        <title>' . htmlspecialchars($title) . '</title>
    </head>
    <body>Some HTML</body>
</html>';
file_put_contents('/path/to/file.html', $html);

这将为您创建一个带有用户提交的标题的文件(假设您通过 POST 提交)。

【讨论】:

  • 我会在这个答案中添加 OP 应该知道目标文件夹权限。 stackoverflow.com/questions/20283626/…
  • XSS 漏洞万岁!
  • 万岁上传后门
  • @Jack 在涵盖 XSS 的 SO 上有 plentyother questions。无需我重复。虽然我已经更新了我的答案以至少防止基础知识。
【解决方案2】:

试试这个:

<?php

$title = htmlspecialchars($_POST['title']);
$content = "<html><head><title>$title</title></head><body>Your content here</body></html>";

file_put_contents('index.html', $content);

【讨论】:

    【解决方案3】:

    所以你的表单需要类似于

    <form action="" method="post">
        <input type="text" name="pageTitle" id="pageTitle" />
        <input type="submit" name="formSubmit" value="Submit" />
    </form>
    

    然后您将需要以下代码来捕获页面标题的值并创建 html 页面

    if($_POST['formSubmit']) {
        $newpage = '<html><head><title>' . $_POST['pageTitle'] . '</title></head><body><-- Whatever you want to put here --></body></html>';
        file_put_contents('newpage.html', $newpage );
    }
    

    【讨论】:

      【解决方案4】:

      您可以像往常一样回显内容,但您应该发送以下标头:

      header('Content-Disposition: attachment; filename=file.html'); //or whatever name you like here
      header('Content-Type: text/html'); //or text/php if the file is php
      

      这将使浏览器为您的文件打开一个下载对话框

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-01
        • 1970-01-01
        • 2014-03-04
        • 2017-10-24
        • 1970-01-01
        • 2011-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多