我正在编码寄存器表单,需要在复选框旁边包含一个PDF文件。PDF文件包含用户在发送表格之前需要同意的重要信息。当您单击复选框旁边出现的文件名时,浏览器必须询问您是否想查看或下载文件。
我如何意识到这一点?
我要使用的方式是创建下载目录" /downloads ",在其中放置一个名为index.php的PHP文件,带有以下代码
<?php
$file = 'http://example.com/download/' . $_GET['file']; //Thanks DanFromGermany!
if($file == 'http://example.com/download/index.php'){die;}
$file_headers = @get_headers($file);
if(substr_count($file_headers[0], '404 Not Found') || substr_count($file_headers[0], '410 Gone')){
exit;
} else {
if($file == 'http://example.com/download/index.php'){die;}
header ("Content-type: octet/stream");
header ("Content-disposition: attachment; filename=".$_GET['file'].";");
header("Content-Length: ".filesize($file));
readfile($file);
header('Location: http://example.com/thanks'); //send them to a thanks page or close the window, whatever works for you
exit;
}
?>
放置要下载的任何文件(read_this.pdf),然后按照以下方式链接它们
<a href="/download?file=read_this.pdf" target="_blank">Download this PDF</a>
您指出的任何文件名都将文件放置在" /downloads "中都会迫使其浏览器下载。
编辑但是我同意Jeroen关于将其放入可滚动Div的建议。为此,文件需要为html,但是您可以使用http://www.pdfthtml.net/之类的东西使HTML看起来像PDF一样。当它试图强迫我下载某些东西时,我个人离开了网站,因为您永远不知道它可能是什么。我仅在给某人下载文件时才使用此方法。否则,希望这就是您要寻找的。