Php 如何强制PDF下载初学者

Php 如何强制PDF下载初学者,php,pdf,header,force-download,Php,Pdf,Header,Force Download,可能重复: 我查看了其他论坛,但我不明白他们说什么。我已经检查了这个链接,人们似乎确实得到了解决方案: 我想做一个按钮,这是下载一个PDF完成,该文件太大,看不到它在线。因此,我希望该按钮强制浏览器下载文件。询问或不询问用户。将有不同的PDF名称文件,所以我需要使这个按钮与各种PDF文件的PDF格式下载 我检查过不同的网站,似乎可以使用htaccess来完成,但我真的不知道该怎么做。谁能帮我看看我怎么做!?!?!有没有一个简单的方法通过CSS/html 为了这么一个小细节,我被困了很长时间

可能重复:

我查看了其他论坛,但我不明白他们说什么。我已经检查了这个链接,人们似乎确实得到了解决方案:

我想做一个按钮,这是下载一个PDF完成,该文件太大,看不到它在线。因此,我希望该按钮强制浏览器下载文件。询问或不询问用户。将有不同的PDF名称文件,所以我需要使这个按钮与各种PDF文件的PDF格式下载

我检查过不同的网站,似乎可以使用htaccess来完成,但我真的不知道该怎么做。谁能帮我看看我怎么做!?!?!有没有一个简单的方法通过CSS/html

为了这么一个小细节,我被困了很长时间

我的代码很简单:

<div class="Container for icons">
<a href="hugefile.pdf" alt=""><img href="icon.png" alt=""/>
</div>


有人能帮我一把吗?

您需要修改返回给客户端的HTTP头,以便完成以下操作:

下面是一个PHP代码示例:

$path = "path/to/file.pdf";
$filename = "file.pdf";
header('Content-Transfer-Encoding: binary');  // For Gecko browsers mainly
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Accept-Ranges: bytes');  // For download resume
header('Content-Length: ' . filesize($path));  // File size
header('Content-Encoding: none');
header('Content-Type: application/pdf');  // Change this mime type if the file is not PDF
header('Content-Disposition: attachment; filename=' . $filename);  // Make the browser display the Save As dialog
readfile($path);  //this is necessary in order to get it to actually download the file, otherwise it will be 0Kb

您可以尝试
ForceType
,而不是
AddType

<FilesMatch "\.(pdf)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

ForceType应用程序/八位组流
标题集内容处置附件

注意:这是一个Apache配置