Use PHP to force Download PDF file examples. Sometimes, when we need to download a PDF file, if it is not processed, the PDF file will be opened directly in the browser, you can save the file by saving it as a file. if you need to download a PDF file, you can open the PDF file in your browser without processing it, then you need to save the downloaded file by saving it as a file. This article uses PHP to directly download PDF files.
Implementation principle: we only need to modify the HTTP header of the page and set Content-Type to force-download to solve the problem.
See the code:
The code is as follows:
ForceDownload ("includemodownload ");
Function forceDownload ($ filename ){
If (false = file_exists ($ filename )){
Return false;
}
// Http headers
Header ('content-Type: application-x/force-download ');
Header ('content-Disposition: attachment; filename = "'. basename ($ filename ).'"');
Header ('content-length: '. filesize ($ filename ));
// For IE6
If (false === strpos ($ _ SERVER ['http _ USER_AGENT '], 'msie 6 ')){
Header ('cache-Control: no-Cache, must-revalidate ');
}
Header ('pragma: no-cache ');
// Read file content and output
Return readfile ($ filename );;
}
For convenience, I wrote a function forceDownload () and then called it.
...