Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Page and Site Features

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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.

as seen on Wordpreneur.com

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.

  1. 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.

  2. 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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

Support Area – Ask Your Question Here

The "Code in articles help" forum at the Willmaster.com support area is the place to get information about implementing JavaScript and other software code found on Willmaster.com

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List
software index page

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC