Php 使html文件可下载

Php 使html文件可下载,php,html,Php,Html,我有一个html文件“sample.html”我需要让用户通过单击页面中的按钮下载该文件 如何使用php将html文件设置为可下载的您可以使用document.documentElement.outerHTML获取页面的html源代码,然后使用FileSystemObject(或其他等效api)将其保存到磁盘。您需要低安全设置才能在没有提示的情况下使其正常工作 下面是一个简单的java脚本(特定于IE): }您可以使用document.documentElement.outerHTML获取页面的

我有一个html文件
“sample.html”
我需要让用户通过单击页面中的按钮下载该文件


如何使用php将html文件设置为可下载的

您可以使用document.documentElement.outerHTML获取页面的html源代码,然后使用FileSystemObject(或其他等效api)将其保存到磁盘。您需要低安全设置才能在没有提示的情况下使其正常工作

下面是一个简单的java脚本(特定于IE):


}

您可以使用document.documentElement.outerHTML获取页面的HTML源代码,然后使用FileSystemObject(或其他等效api)将其保存到磁盘。您需要低安全设置才能在没有提示的情况下使其正常工作

下面是一个简单的java脚本(特定于IE):


}您可以创建这样的页面

// definisco una variabile con il percorso alla cartella // in cui sono archiviati i file $dir = "/root/www/download/"; // Recupero il nome del file dalla querystring // e lo accodo al percorso della cartella del download $file = $dir . $_GET['filename']; // verifico che il file esista if(!file) { // se non esiste chiudo e stampo un errore die("Il file non esiste!"); }else{ // Se il file esiste... // Imposto gli header della pagina per forzare il download del file header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename= " . $file); header("Content-Transfer-Encoding: binary"); // Leggo il contenuto del file readfile($file); } //定义所有可更改的内容 //在cui sono archiviati i文件中 $dir=“/root/www/download/”; //重新命名文件达拉查询字符串 //e lo accodo al percorso della cartella del下载 $file=$dir$_获取['filename']; //验证文件esista 如果(!文件) { //这不是我的错 死(“Il文件非esiste!”); }否则{ //Se il文件esiste。。。 //根据forzare il下载del文件,对gli头文件della pagina进行复制 标头(“缓存控制:公共”); 标题(“内容描述:文件传输”); 标题(“内容处置:附件;文件名=”.$file); 标题(“内容传输编码:二进制”); //Leggo il contenuto del文件 readfile($file); } 然后通过文件名调用它

force-download.php?filename=sample.html force download.php?filename=sample.html
您可以创建这样的页面

// definisco una variabile con il percorso alla cartella // in cui sono archiviati i file $dir = "/root/www/download/"; // Recupero il nome del file dalla querystring // e lo accodo al percorso della cartella del download $file = $dir . $_GET['filename']; // verifico che il file esista if(!file) { // se non esiste chiudo e stampo un errore die("Il file non esiste!"); }else{ // Se il file esiste... // Imposto gli header della pagina per forzare il download del file header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename= " . $file); header("Content-Transfer-Encoding: binary"); // Leggo il contenuto del file readfile($file); } //定义所有可更改的内容 //在cui sono archiviati i文件中 $dir=“/root/www/download/”; //重新命名文件达拉查询字符串 //e lo accodo al percorso della cartella del下载 $file=$dir$_获取['filename']; //验证文件esista 如果(!文件) { //这不是我的错 死(“Il文件非esiste!”); }否则{ //Se il文件esiste。。。 //根据forzare il下载del文件,对gli头文件della pagina进行复制 标头(“缓存控制:公共”); 标题(“内容描述:文件传输”); 标题(“内容处置:附件;文件名=”.$file); 标题(“内容传输编码:二进制”); //Leggo il contenuto del文件 readfile($file); } 然后通过文件名调用它

force-download.php?filename=sample.html force download.php?filename=sample.html
简单地说,您需要读取文件,设置正确的标题并回显文件

header('Content-disposition: attachment; filename=' . $file_name);
header('Content-type: ' . $file_mime);
$file_content = file_get_contents($file_location);
echo $file_content;

将按钮链接到PHP文件,瞧。

简单地说,您需要读取文件,设置正确的标题并回显文件

header('Content-disposition: attachment; filename=' . $file_name);
header('Content-type: ' . $file_mime);
$file_content = file_get_contents($file_location);
echo $file_content;
<?php

$output_file = "example_name.html";

header('Pragma: no-cache"');
header('Expires: 0'); 
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header('Content-Transfer-Encoding: none');

//This should work for IE & Opera
header('Content-Type: application/octetstream; name="' . $output_file . '"');
//This should work for the rest
header('Content-Type: application/octet-stream; name="' . $output_file . '"');

header('Content-Disposition: inline; filename="' . $output_file . '"');

include "your_html_file.html";

exit();

?>

将按钮链接到PHP文件,瞧。

。。。请先搜索,您的问题重复。。。请先搜索,您的问题重复您的代码在opera中不起作用,chrome在下载之前会显示警告。您的代码在opera中不起作用,chrome在下载之前会显示警告。$file\u mime是相应文件的mime类型。HTML文档的MIME类型是“text/HTML”。为了与Opera兼容,您应该添加标题('Content-Type:application/octetstream;name='。$file\u name)$file_mime是对应文件的mime类型。HTML文档的MIME类型是“text/HTML”。为了与Opera兼容,您应该添加标题('Content-Type:application/octetstream;name='。$file\u name);
<?php

$output_file = "example_name.html";

header('Pragma: no-cache"');
header('Expires: 0'); 
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header('Content-Transfer-Encoding: none');

//This should work for IE & Opera
header('Content-Type: application/octetstream; name="' . $output_file . '"');
//This should work for the rest
header('Content-Type: application/octet-stream; name="' . $output_file . '"');

header('Content-Disposition: inline; filename="' . $output_file . '"');

include "your_html_file.html";

exit();

?>