首页 > 代码库 > How to save content/text of a web page by forcing save-as option

How to save content/text of a web page by forcing save-as option

I would like to know what would be the best way to save contents from a web page. I mean to force save-as option by clicking a link or button. I often found that javascript would not be an option because it‘s only working for IE, so I thought that it would be easier because javascript is client-side based.

Now, I thought maybe there is a way to use php when creating a form that will redirect it over a server site query to return and force the save-as option. My main goal is make it possible to save the privacy terms. I would like to avoid using fpdf. If there is someone who could tell how to solve this I would really appreciate it.

UPDATE:

okay to explain what i like to reach:

imagine you have a simple webpage with some privacy policy text and nothing else. at the top of the page you have two options. first will be print option like:

<a href="javascript:print()">print</a>

second should be something like:

<a href="javascript:saveas()">save</a>

so that when you will press the link the save-as dialog pops up and you can download the content or even page to get the privacy policy for your own.

hope it will be clear now. sorry.


If you want to force a download without javascript, simply open the content in a new tab by adding an unknown target.

Example Code

<a href="whatever.com/download.zip" target="_new">click me</a>

This works only if the page you call offers a download, otherwise the content gets displayed. In this case, you need a simple php-file, which gets the content from the page and customizes the header.

<?php
$content = file_get_contents($_GET["downloadurl"]);
header("Content-Type: application/octet-stream"); 
echo $content;
?>

UPDATE

As a more detailed example for your edited informations, you could probably do this:

PHP-File "reader.php"

<?php
$content = file_get_contents($_GET["downloadurl"]);
header("Content-Type: application/octet-stream"); 
echo $content;
?>

HTML on page "test.html"

<a href="reader.php?downloadurl=http://yourwebsite.com/test.html" target="_new">Save website</a>