Download PHP generated webpage as HTML file

download, html, php, save

Solution

<?php
$filename = 'filename.html';
header('Content-disposition: attachment; filename=' . $filename);
header('Content-type: text/html');
// ... the rest of your file
?>

Just put the above code on the top of your PHP file.

Problem

I am using a PHP based CMS which uses `<include>` to add the header and footer of the page, and the content of page is retrieved from a database using PHP/MySQL. I want to create a button on the page that downloads the page as a HTML file - just like what you would get if you copied the page source onto a blank file or saved the page with your browser. Normally I would use PHP to locate the file and download it, as this is a CMS generated page these is no actual file. Does anyone know how to achieve this? (ps - the aim of this is to create a downloadable HTML email file)

Original source