Force Download PDF (Instead of Browser View)
There are situations where a PDF file needs to be downloaded to the site visitor's computer. Not just loaded into the browser.
These might be sales tools, how-to manuals, price lists, any PDF document to be shared independently or referred to repeatedly.
Loading a PDF into the browser generally also provides a way to save the document to the person's computer. But it's an extra step. Some simply won't do that extra step (or don't know how). The site visitor could also right-click to download the file, but not all will do that or know it can be done.
If it needs to be downloaded, be friendly and make it easy to download with a click or a tap.
Further below is a short PHP script to help you implement exactly that. The script initiates a file download instead of sending the file to the browser window.
It's similar to how Download Handler works, except Download Handler (among other things) counts downloads, can keep file locations secret, and is not restricted to PDF files.
The PHP script further below is simple download-one-file software without the other features Download Handler has. Note that downloads are restricted to file names ending as ".pdf".
How It Works
The Download-a-File software further below is a PHP script you'll upload to your server.
-
In your web page, link to the download script. The URL of the download script is followed with a "?" and the location of the file to download.
-
When the link is clicked, the download script causes the file to be downloaded.
That's it. Here's an example link:
<a href="http://example.com/DownloadAFile.php?/download/something.pdf"> Download the "something" document </a>
The Download-a-File Software
Here's the Download-a-File source code. No modifications necessary. Simply name it DownloadAFile.php and upload it to your server.
<?php /* Download-a-File Version 1.2 May 12, 2022 Version 1.0 - original beta version - March 29, 2015 Version 1.1 - enforce leading "/" in file location, bug fix, code tweaks - March 30, 2015 Version 1.2 - download restricted to file names ending as ".pdf" - May 12, 2022 Will Bontrager Software LLC https://www.willmaster.com/ Copyright 2015, 2022 Will Bontrager Software LLC This software is provided "AS IS," without any warranty of any kind, without even any implied warranty such as merchantability or fitness for a particular purpose. Will Bontrager Software LLC grants you a royalty free license to use or modify this software provided this notice appears on all copies. */ if( empty($_SERVER['QUERY_STRING']) ) { ExitWithMessage('Inappropriate access.'); } $location = $downloadfile = trim(urldecode($_SERVER['QUERY_STRING'])); if( ! preg_match('/\.pdf$/i',$downloadfile) ) { ExitWithMessage('Only appropriately-named PDF files may be downloaded.'); } if( strpos($downloadfile,'/') !== 0 ) { $downloadfile = "/$downloadfile"; } $downloadfile = "${_SERVER['DOCUMENT_ROOT']}$downloadfile"; if( ! file_exists($downloadfile) ) { ExitWithMessage("Unable to find file $location"); } if( ! ($filesize = filesize($downloadfile)) ) { ExitWithMessage("File $location seems to be empty."); } $ta = explode('/',$downloadfile); $downloadfilename = array_pop($ta); header("Pragma: private"); header("Expires: 0"); header("Cache-Control: private, must-revalidate"); header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"".$downloadfilename."\""); header("Content-Transfer-Encoding: binary"); header("Content-Length: $filesize"); readfile($downloadfile); exit; function ExitWithMessage($s) { echo $s; exit; } ?>
The file may be named something other than DownloadAFile.php so long as it has a .php file name extension.
Use the Download-a-File software by linking to it. Append a "?" character to the software URL followed by the location of the file to download. The location must be the URI relative to the document root. As an example, if the file's URL is http://example.com/place/file.php then the file's location is /place/file.php
The software will respond with an error message if any of these conditions exist:
- No file was specified.
- The specified file doesn't exist on the server.
- The specified file is empty.
- The requested file name does not end with ".pdf".
Otherwise, the software will cause the file to be downloaded. The browser remains on the page with the link.
On your web pages, link to DownloadAFile.php with the location of the file to download. Kinda like magic, the file downloads when the site visitor clicks the link or, for mobile devices that can download, taps the link.
(This article first appeared in Possibilities ezine.)
Will Bontrager