Home > Article > Backend Development > PHP makes the browser download the pdf instead of opening it_PHP tutorial
This article will introduce to you how to allow users to open PDF files instead of browsing them directly in the browser but directly downloading the PDF files to the client in the form of downloaded files.
Some browsers have a pdf opening program installed and associated with the browser, so when you directly write the pdf path, the pdf will be opened instead of downloaded. Below I will talk about how to let them just download instead of browse.
Create a php file
代码如下 | 复制代码 |
$file = $_GET['file']; $arr = explode(‘/’,$file); header(‘Content-type: application/pdf’); header(‘Content-Disposition: attachment; filename=”‘.array_pop($arr).’”‘); readfile(file); ?> |
Add code to convert the pdf path into a language recognized by the browser and tell the browser
Liezi /upload.php?file=/aa/aa.pdf
In this way, no matter what it is, it will be downloaded instead of opening