If you're new here, you may want to subscribe to my RSS feed. So that you can read the latest updates about Web2.0 tools, Making Money Online, Tips in SEO, Ajax and many more. Thanks for visiting ProgramimiCOM!

This article will show you how to make a link to download any file on your server without revealing the file’s location.

It’s a one-click download link.

But, why?

  1. For short-term promotions, after which the link can be removed, leaving the orignal file where it’s at.
  2. Maybe you’re offering a link to download a real working script.
  3. It can come in handy to provide access to an intermittently updated file without providing FTP login information.
  4. Files not otherwise accessible by browser can be downloaded.
  5. It’s also very handy for personal use, instead of FTP, for logs or other files frequently downloaded. The download link would be on a web page stored on the hard drive, not on the Internet.

The download link requires software on your server to send the file contents to the browser. Complete software for PHP and for Perl come with the article.

Choose which you prefer to use. Only one is needed.

Here is the PHP code:

<?php
# Please link to http://BontragerConnection.com/ with the text,
# “Download software provided by Bontrager Connection, LLC”
#
# This block of PHP code must be at the top of the file.
# No space or blank lines may be above it.
#
# Specify the location and name of the file to be downloaded.
$FileLocation = “/path/to/file.zip”;

# Specify the file name as it is to be represented when downloading.
$DownloadFileAs = “file.zip”;

header(’Content-Type: application/force-download’);
header(’Content-Disposition: attachment; filename=”‘.$DownloadFileAs.’”‘);
header(’Content-Transfer-Encoding: binary’);
header(’Expires: Mon, 26 Jul 1997 05:00:00 GMT’); # Date in the past.
header(’Cache-Control: no-cache, must-revalidate’);
header(’Cache-control: private’);
if(! @readfile(”$FileLocation”) )
{
$FileLocation = preg_replace(’/^\/+/’,”,$FileLocation);
@readfile($_SERVER[’DOCUMENT_ROOT’].”/$FileLocation”);
}
?>

And, here is the Perl code:

#!/usr/bin/perl
# Please link to http://BontragerConnection.com/ with the text,
# “Download software provided by Bontrager Connection, LLC”
#
# Specify the location and name of the file to be downloaded.
my $FileLocation = “/path/to/file.zip”;

# Specify the file name as it is to be represented when downloading.
my $DownloadFileAs = “file.zip”;

print “Cache-control: private\n”;
print “Content-Type: application/force-download\n”;
print “Content-Disposition: attachment; filename=\”$DownloadFileAs\”\n”;
print “Content-Transfer-Encoding: binary\n\n”;
unless(open R,”<$FileLocation”)
{
$FileLocation =~ s!^/+!!;
open R,”<$ENV{DOCUMENT_ROOT}/$FileLocation”;
}
binmode R;
my $buffer = ”;
while(read(R,$buffer,1024)) { print $buffer; }
close R;
# end of script
When you look at the code of either the PHP or the Perl script, you’ll see two places to customize. The first is the value of the $FileLocation variable and the second is the value of the $DownloadFileAs variable.

The value of $FileLocation is the location of the file on your server that will be downloaded. Specify the directory path with the file name. The file may be located anywhere on your server, even in a password protected directory.

The value of $DownloadFileAs is the file name that you want to have the file downloaded as. This is likely to be the same name (minus the path information) as the file being downloaded. But it can be different. Your downloadable file might be version-B-file.zip and you may want it downloaded as file.zip, instead.

When the customization has been completed with the script of your choice, go ahead and install it.

If installing the PHP script, verify the PHP code is at the beginning of the file. No spaces or other characters, and no blank lines, may come before the PHP code. Otherwise, the PHP code will be unable to send header lines to the browser. Upload it to somewhere in your document directories.

If installing the Perl script, upload it to your server as a plain text file (not binary), in a directory that can run Perl scripts. Then, give the script 755 permissions.

In either case, the URL for your download link is the URL to the script you installed. Construct the download link just like you would any other link.

When the link is clicked, the download commences. In most situations, the user will be asked where on their computer to store the file. The web page itself does not change.

Notes:

If the downloaded file is of zero size, the script was unable to open the file. Verify the value of $FileLocation is correct.

If the script can’t open the file at the location specified, it will try to open the file relative to the document root directory. If it still can’t open the file, nothing will be downloaded (resulting in a download file of zero size).