views:

97

answers:

4

What approach could someone suggest to save the current page as an HTML file to the server? In this case, also note that security is not an issue.

I have spent endless hours searching around for this, and have not found a single thing.

Your help is much appreciated, thank you!

Edit

Thank you all for your help, it was very much appreciated.

+1  A: 

Use JavaScript to send document.getElementsByTagName('html')[0].innerHTML as hidden input value or by ajax to the server side. This is more useful than output buffering if the content is afterwards traversed/modified by JavaScript, which the server side might not have any notion about.

BalusC
Thanks, BalusC. So if I use var $s = document.getElements... (in php) I can then write the whole var to a file on the server?
lucifer
JavaScript runs at webbrowser, not at webserver. Do you know JS? Anyway, given your comment I think this answer is after all not what you need :) You probably rather want to save the immediate PHP-generated HTML page, not the currently opened HTML page (in all its current client side state). Check Holyvier's answer.
BalusC
Thank you BalusC :)
lucifer
+4  A: 

If you meant saving the output of a page in a file, you can use buffering to do that. The function you need to use are ob_start and ob_get_contents.

<?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());
?>

This will save the content of the page in the file yourpage.html.

HoLyVieR
+1  A: 

I think we can use Output Control Functions of PHP, you can use save the content to the variable first and then save them to the new file, next time, you can test it the html file exists, then render that else re-generate the page.

<?php
$cacheFile = 'cache.html';

if ( (file_exists($cacheFile)) && ((fileatime($cacheFile) + 600) > time()) )
{
    $content = file_get_contents($cacheFile);
    echo $content;
} else
{
    ob_start();
    // write content
    echo '<h1>Hello world to cache</h1>';
    $content = ob_get_contents();
    ob_end_clean();
    file_put_contents($cacheFile,$content);
    echo $content;
}
?>

Example taken from : http://www.php.net/manual/en/function.ob-start.php#88212

Chetan sharma
Thank you Chetan :)
lucifer
You are welcome..
Chetan sharma
+1  A: 

I feel you need curl, so that you can save any pages' output. Use curl with returntransfer true. and do whatever you want with the output.

Satya Prakash
Thank you, Satya. :)
lucifer