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

时间:2015-04-03 10:17:39

标签: php html save server

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

使用php,如何将页面的当前html保存到Web服务器上的.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());
?>

如果有人能指引我朝着正确的方向前进,我会永远爱你。

编辑:代码有点长,可能非常格式化,但我正在创建一个简单的时间表单,我知道顶级表是目前唯一可用的表。我决定推迟改变所有其他人,直到我弄清楚。

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>
"

3 个答案:

答案 0 :(得分:1)

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

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

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

的内容

答案 1 :(得分:0)

您的代码段是正确的,也许您的php.ini文件中禁用了输出缓冲。

答案 2 :(得分:0)

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

    $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()