Reputation: 75
So I've been banging my head on my keyboard for the last 6 hours trying to figure this out.
Using php, how can I save a page's current html to a .html file on the web server?
This code I've found seems to be exactly what I need but I can't, for the life of me, figure out why its not working. The page loads in the browser just fine, just nothing saves, anywhere.
<?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());
?>
If someone could kindly guide me in the right direction I would love you forever.
edit: Code is kind of long and possibly terribly formatted, but I am creating a simple time sheet form, also, I know the top table is the only one that will work at this time. I decided to hold off on changing all the rest of them until I could figure it out.
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> </td>
<td> </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>
"
Upvotes: 1
Views: 1185
Reputation: 4889
If output buffering doesn't work for you and you can't get it fixed, you can always switch to an approach where you add your HTML into a PHP variable and echo it out in the end.
$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;
...and then simply save the $HTML
variable contents in place of using ob_get_contents()
.
Upvotes: 0
Reputation: 7785
Ok, your buffering seems to work fine
Try with this code :
$res = file_put_contents('C:\yourpage.html', ob_get_contents());
var_dump($res);
My response, is to write your data in a file that is located in absolute path
Also, i need the contents of $res
Upvotes: 1
Reputation: 92
Your code snippet is correct, maybe the output buffering is disabled in your php.ini file.
Upvotes: 0