如何将当前 html 保存到服务器上的文件?

How to save current html to file on server?

所以在过去的 6 个小时里,我一直在敲键盘,试图解决这个问题。

如何使用 php,将页面的当前 html 保存到网络服务器上的 .html 文件中?

我发现的这段代码似乎正是我所需要的,但我终究无法弄清楚为什么它不起作用。该页面在浏览器中加载得很好,只是没有任何保存,在任何地方。

<?php
// Start the buffering //
ob_start();
?>
Your page content bla bla bla bla ...

<?php
echo '1';

// Get the content that is in the buffer and put it in your file //
file_put_contents('yourpage.html', ob_get_contents());
?>

如果有人能指导我正确的方向,我会永远爱你。

编辑:代码有点长而且格式可能非常糟糕,但我正在创建一个简单的时间 sheet 表单,而且,我知道顶部 table 是唯一可以工作的这次。在我弄清楚之前,我决定推迟更改所有其他的。

string(4642) "<html>
<body>
<b><font size="6">Timesheet</font></b>

<table>
    <tr>
        <td><b>Day of Week</b></td>
        <td><b>Week 1 Hours</b></td>
        <td><b>Week 2 Hours</b></td>
    </tr>
    <tr>
        <td>Monday</td>
        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
    </tr>
    <tr>
        <td>Tuesday</td>
        <td><input type="text" name="Tuesday" size="3" readonly="readonly" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"><input type="checkbox" tabindex="-1" name="Stime3">Sick?<input type="checkbox" tabindex="-1" name="Vac3">Vacation?</td>
        <td><input type="text" name="Tuesday2" size="3" readonly="readonly" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"><input type="checkbox" tabindex="-1" name="Stime4">Sick?<input type="checkbox" tabindex="-1" name="Vac4">Vacation?</td>
    </tr>

    <tr>
        <td>Wednesday</td>
        <td><input type="text" name="Wednesday" size="3" readonly="readonly" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"><input type="checkbox" tabindex="-1" name="Stime5">Sick?<input type="checkbox" tabindex="-1" name="Vac5">Vacation?</td>
        <td><input type="text" name="Wednesday2" size="3" readonly="readonly" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"><input type="checkbox" tabindex="-1" name="Stime6">Sick?<input type="checkbox" tabindex="-1" name="Vac6">Vacation?</td>
    </tr>

    <tr>
        <td>Thursday</td>
        <td><input type="text" name="Thursday" size="3" readonly="readonly" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"><input type="checkbox" tabindex="-1" name="Stime7">Sick?<input type="checkbox" tabindex="-1" name="Vac7">Vacation?</td>
        <td><input type="text" name="Thursday2" size="3" readonly="readonly" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"><input type="checkbox" tabindex="-1" name="Stime8">Sick?<input type="checkbox" tabindex="-1" name="Vac8">Vacation?</td>
    </tr>

    <tr>
        <td>Friday</td>
        <td><input type="text" name="Friday" size="3" readonly="readonly" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"><input type="checkbox" tabindex="-1" name="Stime9">Sick?<input type="checkbox" tabindex="-1" name="Vac9">Vacation?</td>
        <td><input type="text" name="Friday2" size="3" readonly="readonly" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"><input type="checkbox" tabindex="-1" name="Stime10">Sick?<input type="checkbox" tabindex="-1" name="Vac10">Vacation?</td>
    </tr>

        <tr><td>Saturday</td>
        <td><input type="text" name="Saturday" size="3" readonly="readonly" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"><input type="checkbox" tabindex="-1" name="Stime11">Sick?<input type="checkbox" tabindex="-1" name="Vac11">Vacation?</td>
        <td><input type="text" name="Saturday2" size="3" readonly="readonly" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"><input type="checkbox" tabindex="-1" name="Stime12">Sick?<input type="checkbox" tabindex="-1" name="Vac12">Vacation?</td>
    </tr>

    <tr>
        <td>Sunday</td>
        <td><input type="text" name="Sunday" size="3" readonly="readonly" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"><input type="checkbox" tabindex="-1" name="Stime13">Sick?<input type="checkbox" tabindex="-1" name="Vac13">Vacation?</td>
        <td><input type="text" name="Sunday2" size="3" readonly="readonly" maxlength="4" value="" onkeypress="return inputLimiter(event,'Numbers')"><input type="checkbox" tabindex="-1" name="Stime14">Sick?<input type="checkbox" tabindex="-1" name="Vac14">Vacation? </td>
    </tr>

    <tr>
        <td><b>Week 1 Total</td>
        <td><input class="right" type="number" name="Wk1Total" readonly="readonly" size="5" value=""></td>
    </tr>

    <tr>
        <td><b>Week 2 Total</td>
        <td><input class="right" type="number" name="Wk2Total" readonly="readonly" size="5" value=""></td>
    </tr>

    <tr>
        <td><b>Overtime Hours</td>
        <td><input class="right" type="number" name="OT" readonly="readonly" size="5" value=""></td>


    </tr>

    <tr>
        <td><b>Sick Time Used</td>
        <td><input class="right" type="text" name="ST" size="5" value="" onkeypress="return inputLimiter(event,'Numbers')"></td>
    </tr>

    <tr>
        <td><b>Vacation Used</td>
        <td><input class="right" type="text" name="VT"  size="5" value="" onkeypress="return inputLimiter(event,'Numbers')"></td>
    </tr>

</table>
<b>Notes:</b>
<br>
<textarea name="comments" cols="73" rows="8">
</textarea><br>
<br>
<iframe width="1" height="1" frameborder="0" src=""></iframe>
</body>
</html>
"

您的代码片段是正确的,可能在您的 php.ini 文件中禁用了输出缓冲。

好的,你的缓冲似乎工作正常
试试这个代码:

$res = file_put_contents('C:\yourpage.html', ob_get_contents());
var_dump($res);

我的回应是将您的数据写入位于绝对路径的文件中
另外,我需要 $res

的内容

如果输出缓冲对您不起作用并且您无法修复它,您可以随时切换到将 HTML 添加到 PHP 变量并回显它的方法终于出来了。

    $HTML = <<<QWE
<p>Some HTML here.</p>
<p>Some more...</p>
QWE;

// And append more HTML...
$HTML .= '<b>FooBar</b>';

// Output at the end of your file:
echo $HTML;

...然后简单地保存 $HTML 变量内容代替使用 ob_get_contents()