Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save current page as HTML to server

Tags:

html

file

php

save

People also ask

How do I save an HTML file automatically?

1) Select the tab you want to auto save (only 1 tab can be autosaved), open the Autosave extension, click + and it will show the selected tab. 2) Choose a sub-directory from the Google Downloads directory (only Downloads and sub-directories can be saved to with a Chrome extension). Leave blank to save to Downloads.

How do I save HTML as HTML?

Choose File > Save As and choose HTML from the drop-down list. Give the filename an extension of . html, specify the file location, and click Save.

How do I save a webpage as HTML and CSS?

Open up the webpage and click File-> Save Page As... and from that prompt select "Web Page, Complete" . Once you've saved this page this downloads a complete version of the html, javascript, css files and images that are referenced in the HTML.


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.


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


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.


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