Download PHP generated webpage as HTML file

≯℡__Kan透↙ 提交于 2019-12-21 05:05:08

问题


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)


回答1:


<?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.




回答2:


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...";

?>


来源:https://stackoverflow.com/questions/13080943/download-php-generated-webpage-as-html-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!