Possible Duplicate:
open download dialog with php可能重复:用php打开下载对话框
I have a link in my page say, <a href='test.pdf'>(Test.pdf)</a>
. When I click on that link, download dialogue box should open to download that file. Can anyone help me in implementing this in PHP?
我的页面中有一个链接,(Test.pdf)。当我点击该链接时,应打开下载对话框以下载该文件。任何人都可以帮助我在PHP中实现这一点吗?
thanks
3 个解决方案
#1
34
$filename = 'Test.pdf'; // of course find the exact filename....
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'. basename($filename) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit;
Name the above file as download.php
将上述文件命名为download.php
HTML:
<a href="download.php">Test.pdf</a>
That should do it.
应该这样做。
#2
15
<a href="test.pdf">test.pdf</a>
#3
2
In the case of a PDF file, most browsers are going to look for the helper (acrobat) to load it in your browser by default. You are trying to get around this default behavior is my guess.
对于PDF文件,大多数浏览器默认会在浏览器中查找帮助程序(acrobat)。你试图绕过这种默认行为是我的猜测。
The easiest way to do this (assuming you're on *nix box with apache) is to make an .htaccess file in the directory you want to have this result and add the line:
最简单的方法(假设您使用的是带有apache的* nix框)是在要获得此结果的目录中创建.htaccess文件并添加以下行:
AddType application/octet-stream .pdf
AddType application / octet-stream .pdf
This will cause any file with the extention .pdf to download by default. You can even have some .pdf files on the page load in the browser while others download by using the FilesMatch directive ( http://www.askapache.com/htaccess/using-filesmatch-and-files-in-htaccess.html ).
这将导致任何具有扩展名.pdf的文件默认下载。您甚至可以在浏览器中加载页面上的一些.pdf文件,而其他文件则使用FilesMatch指令下载(http://www.askapache.com/htaccess/using-filesmatch-and-files-in-htaccess.html) 。
I realize your original question said "how do I do it with PHP" but I thought I'd post in case you were looking for a simpler, more elegant solution. Do keep in mind any directives you put in an .htaccess file will also affect any sub-directories below it.
我意识到你原来的问题是“我怎么用PHP做”但我想我会发帖以防你想要一个更简单,更优雅的解决方案。请记住,您放在.htaccess文件中的任何指令也会影响它下面的任何子目录。
#1
34
$filename = 'Test.pdf'; // of course find the exact filename....
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'. basename($filename) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit;
Name the above file as download.php
将上述文件命名为download.php
HTML:
<a href="download.php">Test.pdf</a>
That should do it.
应该这样做。
#2
15
<a href="test.pdf">test.pdf</a>
#3
2
In the case of a PDF file, most browsers are going to look for the helper (acrobat) to load it in your browser by default. You are trying to get around this default behavior is my guess.
对于PDF文件,大多数浏览器默认会在浏览器中查找帮助程序(acrobat)。你试图绕过这种默认行为是我的猜测。
The easiest way to do this (assuming you're on *nix box with apache) is to make an .htaccess file in the directory you want to have this result and add the line:
最简单的方法(假设您使用的是带有apache的* nix框)是在要获得此结果的目录中创建.htaccess文件并添加以下行:
AddType application/octet-stream .pdf
AddType application / octet-stream .pdf
This will cause any file with the extention .pdf to download by default. You can even have some .pdf files on the page load in the browser while others download by using the FilesMatch directive ( http://www.askapache.com/htaccess/using-filesmatch-and-files-in-htaccess.html ).
这将导致任何具有扩展名.pdf的文件默认下载。您甚至可以在浏览器中加载页面上的一些.pdf文件,而其他文件则使用FilesMatch指令下载(http://www.askapache.com/htaccess/using-filesmatch-and-files-in-htaccess.html) 。
I realize your original question said "how do I do it with PHP" but I thought I'd post in case you were looking for a simpler, more elegant solution. Do keep in mind any directives you put in an .htaccess file will also affect any sub-directories below it.
我意识到你原来的问题是“我怎么用PHP做”但我想我会发帖以防你想要一个更简单,更优雅的解决方案。请记住,您放在.htaccess文件中的任何指令也会影响它下面的任何子目录。