Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

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!

Real Download Link

Browser windows are especially adept at displaying the content of certain types of files. Image files, for example. And text files.

Some files are made for viewing in other software but, depending on the browser, can be viewed in a browser window. PDF files are an example.

What if you want to link to a file but have it downloaded to the user's hard drive instead of viewed in a browser window?

You can tell the browser to do that.

The browser doesn't have to follow your request to download the file being linked to. But you can tell it to.

If the browser is running on a device with no download storage capacity, it is unlikely that the browser will try to download the file to the user's hard drive. Browser preference settings might also prevent it from downloading.

One way to tell a browser to download a file instead of displaying the content of the file in a browser window is to link to a short PHP script on your server and tell it the location of the file to download. Here is the script:

<?php /*
Force Download From Link
Version 1.0
May 2, 2025
Will Bontrager Software LLC
https://www.willmaster.com/
*/
if( empty($_SERVER['QUERY_STRING']) ) { exit; }
$location = trim(urldecode($_SERVER['QUERY_STRING']));
$ta = explode('/',$location);
$downloadfilename = array_pop($ta);
if( ! preg_match('!^https?://!i',$location) ) { $location = "{$_SERVER['DOCUMENT_ROOT']}$location"; }
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$downloadfilename."\"");
header("Content-Transfer-Encoding: binary");
echo(file_get_contents($location));
exit;
?>

Save the above source code and upload it to your server. Call it forcedownload.php or other *.php file name that is more suitable for you. Make a note of the script's URL.

For the following instructions on how to make a download link, let's make two assumptions: Your forcedownload.php script URL is https://example.com/forcedownload.php and the file to download is at URL https://example.com/test/myfile.txt

Here is link source code that incorporates the above two assumptions.

<a href="https://example.com/forcedownload.php?https://example.com/test/myfile.txt">Download</a>

In the above code you see that the link URL is composed of two parts with a ? between them. Like [ONE]?[TWO]

[ONE] is the URL of your forcedownload.php script. [TWO] is the URL of the file to download.

Now that you know how to construct the link, I will mention a couple optional variations.

  1. If your forcedownload.php script is at the same website as the web page with the link, the script URL optionally may be a relative URL. To make a relative URL, remove the leading https:// and domain name. In the example link code, the relative URL for forcedownload.php is /forcedownload.php

  2. If the download file is at the same website as the forcedownload.php script, the download file URL optionally may be a relative URL. Same as in the previous step, to make a relative URL, remove the leading https:// and domain name. In the example link code, the relative URL for the download file is /test/myfile.txt

To implement your ability to create links that tell the browser to download the file, install the PHP script on your server. Make a note of its URL.

Then, to do the actual linking, the link URL is composed of the PHP script's URL, followed by a ? character, and completed with the download file's URL.

(This content first appeared in Possibilities newsletter.)

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.

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

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