40

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
    you asked the same question just 14 hour ago: [Saving Source of Self (PHP)] - why don't you try to get an answer there instead of posting it again?(http://stackoverflow.com/questions/3769504/saving-source-of-self-php) – oezi Sep 23 '10 at 04:46
  • 1
    oezi - Well, it looks like I was right about creating this new question. I got a correct answer... New question, different people, different answers. You can go ahead and close this question now. –  Sep 23 '10 at 05:35
  • These questions aren't exactly the same. They are very similar but differ in the target: here it's the server, in the other question it's the browser itself. – nalply Aug 03 '12 at 22:09

6 Answers6

72

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
  • 10,985
  • 5
  • 42
  • 67
9

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
  • 2,539
  • 5
  • 25
  • 41
4

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
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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? –  Sep 23 '10 at 03:35
  • 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 Sep 23 '10 at 03:37
3

In case you are looking to save complete html page along with css, images and scripts in a single html file, you can use this class I have written:

This class can save HTML pages complete with images, CSS and JavaScript.

It takes the URL of a given page and retrieves it to store in a given file.

The class can parse the HTML and determine which images, CSS and JavaScript files it needs, so those files are also downloaded and saved inside the HTML page saved to a local file.

Optionally it can skip the JavaScript code, keep only the page content, and compress the resulting page removing the whitespace.

http://www.phpclasses.org/package/8305-PHP-Save-HTML-pages-complete-with-images-CSS-and-JS.html

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
1

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
  • 3,372
  • 3
  • 31
  • 47
1
//function to use curl to get the content of the page.
//parameter used url and $data for the posting credentials to retrieve information.

function httpPost($url, $data){
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

//
$filename="abc.html"; // whatever name you want.
$myfile = fopen($filename, "w") or die("Unable to open file!");
$txt =  httpPost(<url>, ""); //<url> replace by url you want.
fwrite($myfile, $txt);
fclose($myfile);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
shaan gola
  • 109
  • 1
  • 4
  • 1
    This is a great suggestion but appears you must enter a complete URL ($txt = httpPost(, "") ) instead of using a local file. maybe show us how to get a local file instead of remote URL? – Woody Dec 08 '18 at 13:53