Home  >  Article  >  php教程  >  php使浏览器直接下载pdf文件的方法

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

WBOY
WBOYOriginal
2016-06-06 20:26:241097browse

本文介绍如何让用户打开pdf文件时不是直接在浏览器浏览而是直接以下载文件形式把pdf文件下载到电脑

有的浏览器安装了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