下载PHP生成的网页作为HTML文件

时间:2021-09-26 09:48:01

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.

我正在使用一个基于PHP的CMS,它使用 添加页面的页眉和页脚,并且使用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.

我想在页面上创建一个按钮,将页面下载为HTML文件——就像您将页面源复制到空白文件或使用浏览器保存页面时所得到的一样。

Normally I would use PHP to locate the file and download it, as this is a CMS generated page these is no actual file.

通常我会使用PHP来定位和下载文件,因为这是一个CMS生成的页面,这些不是实际的文件。

Does anyone know how to achieve this?

有人知道怎么做到吗?

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

(ps -这是为了创建一个可下载的HTML电子邮件文件)

2 个解决方案

#1


7  

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

把上面的代码放在PHP文件的顶部。

#2


5  

You can try this with file_get_contents();

您可以尝试使用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...";

?>

#1


7  

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

把上面的代码放在PHP文件的顶部。

#2


5  

You can try this with file_get_contents();

您可以尝试使用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...";

?>