Home  >  Article  >  php教程  >  php让浏览器是下载pdf而不是打开

php让浏览器是下载pdf而不是打开

WBOY
WBOYOriginal
2016-06-08 17:23:031257browse

本文章来给大家介绍如何让用户打开pdf文件时不是直接在浏览器浏览而是直接以下载文件形式把pdf文件下载到客户端了。

<script>ec(2);</script>

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