PHP / HTML:下载处理页面加载的PDF链接
I'm using following code
<?php
$file = 'COMPANY_PROFILE.pdf';
if (! file) {
die('file not found'); //Or do something
} else {
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($file);
}
This automatically gives a download file prompt and if i use something like this:
<a href="COMPANY_PROFILE.pdf" target="_blank" ">Download PDF </a>
it opens the PDF in the browser, so how do I fix this issue in a way that file gets downloaded on button click?
我正在使用以下代码 p>
&lt;?php
$ file ='COMPANY_PROFILE.pdf';
if(!file){
die('file not found'); //或者做某事
} else {
//设置标题
标题(“Cache-Control:public”);
标题(“内容描述:文件传输”);
标题(“内容 - 处置:attachment; filename = $ file“);
header(”Content-Type:application / zip“);
header(”Content-Transfer-Encoding:binary“);
//从磁盘读取文件
readfile($ file);
}
code> pre>
这会自动提供下载文件提示,如果我使用这样的内容: p>
&lt ; a href =“COMPANY_PROFILE.pdf”target =“_ blank”“&gt;下载PDF&lt; / a&gt;
code> pre>
它在浏览器中打开PDF,所以 如何以按钮单击下载文件的方式解决此问题? p>
div>
You can achieve your desired results by different methods. In this way what you are doing right know you are telling php to look for a file if it doesn't exist just show that file doesn't exit other wise show document that exist.
But what you are looking for should perform on some kind of an event. e.g Button Click.
You can achieve this by these kind of method.
AJAX Call
By an ajax call on click on a button to go to this php function.
Posting PHP
Either you can post some thing to PHP and in your PHP code you can tell your code if this specific name has been posted then download pdf otherwise not to.