Home  >  Article  >  Backend Development  >  php使浏览器直接下载pdf文件的方法_PHP

php使浏览器直接下载pdf文件的方法_PHP

WBOY
WBOYOriginal
2016-06-01 11:57:382611browse

有的浏览器安装了pdf打开程序关联到浏览器,所以直接写上pdf路径时是打开pdf而不是下载,下面我就说下如果让他们只是下载,而不是浏览。

创建一个php文件

复制代码 代码如下:
$file = $_GET['file'];
$arr = explode('/',$file);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="'.array_pop($arr).'"');
readfile(file);
?>
 

加入代码,通过这个把pdf路径转换成浏览器认识的语言,告诉浏览器
列子 /upload.php?file=/aa/aa.pdf
这样就可以不管是什么,都会下载,而不是打开

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn