Download the PHP version of the web page as an HTML file

I am using PHP-based CMS, which uses <include> to add the page header and footer, and the contents of the page are retrieved from the database using PHP / MySQL.

I want to create a button on a page that loads the page as an HTML file - just as if you copied the page source to an empty file or saved the page in your browser.

Normally I would use PHP to find the file and load it, since this is a CMS page created, it is not the actual file.

Does anyone know how to achieve this?

(ps - the purpose of this is to create a downloadable HTML email file)

+4
source share
2 answers
 <?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 at the top of your PHP file.

+7
source

You can try this with file_get_contents ();

 <?php //Link to download file... $url = "http://example.com/test.php"; //Code to get the file... $data = file_get_contents($url); //save as? $filename = "test.html"; //save the file... $fh = fopen($filename,"w"); fwrite($fh,$data); fclose($fh); //display link to the file you just saved... echo "<a href='".$filename."'>Click Here</a> to download the file..."; ?> 
+5
source

Source: https://habr.com/ru/post/1442240/


All Articles