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

WillMaster > LibraryStatistics and Tracking

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!

Download Logger and Counter

When you have a file for people to download, they need a way to do so.

The PHP code and HTML meta tag on this page together provide a way.

The code and meta tag make any web page into a download page. When the browser lands on the download page, the download is initiated automatically.

What happens when the download is automatically initiated is the same as would happen if the person tapped on a link to a file:

  • If the file is a *.zip or other file with a file name extension that browsers automatically download, the download can happen without the browser leaving the page.

  • Otherwise, the page in the browser window will be replaced with the content of the file. The user can then decide whether or not to download the file currently displayed in the browser window.

What the PHP code and HTML meta tag do is make it so the user doesn't have to tap a link — it just happens automatically.

Plus, with this system, the download may optionally be logged as a CSV file (date/time, IP address, …). And, also optionally, the download may be counted.

To implement the auto-download functionality, you create a web page for this purpose. The web page may contain whatever content you want — instructions, announcements, a video, whatever.

In the HEAD area of your web page, you pop in the PHP script and an HTML meta tag to automatically initiate the download.

Implementing Automatic Download

In the HEAD area of the download web page source code, insert the following PHP script and HTML meta tag. (Notes follow.)

<?php
/*
   Download Logger and Counter
   Version 1.0
   November 11, 2019
   Will Bontrager Software LLC
   https://www.willmaster.com/
*/

/* *** Customizations *** */
// 3 places to customize:

// Place 1:
// The URL of the downloadable file.
$URLofDownloadableFile = "/downloads/thedoc.zip";

// Place 2:
// Optionally, the location of the download log CSV file. Otherwise, leave blank.
$LocationOfLogFile = "/downloads/log/log.csv";

// Place 3:
// Optionally, the location of the download count file. Otherwise, leave blank.
$LocationOfCountFile = "/downloads/log/count.txt";

/* *** End of Customizations *** */

if( (!empty($LocationOfCountFile)) and strlen($LocationOfCountFile)>2 )
{
   if( strpos($LocationOfCountFile,'/')===0 ) { $LocationOfCountFile = "{$_SERVER['DOCUMENT_ROOT']}$LocationOfCountFile"; }
   file_put_contents($LocationOfCountFile,'x',FILE_APPEND);
}
if( (!empty($LocationOfLogFile)) and strlen($LocationOfLogFile)>2 )
{

   if( strpos($LocationOfLogFile,'/')===0 ) { $LocationOfLogFile = "{$_SERVER['DOCUMENT_ROOT']}$LocationOfLogFile"; }
   $LogCell = array();
   $LogCell[] = date('r');
   $LogCell[] = $_SERVER['REMOTE_ADDR'];
   $LogCell[] = str_replace('"','""',$URLofDownloadableFile);
   $LogCell[] = str_replace('"','""',$_SERVER['HTTP_USER_AGENT']);
   file_put_contents($LocationOfLogFile,'"'.implode('","',$LogCell)."\"\n",FILE_APPEND);
   $filehandle = fopen($LocationOfLogFile,'a');
}
?>
<meta http-equiv="refresh" content="0; url=<?php echo($URLofDownloadableFile) ?>">

Notes:

The above code box contains PHP code and, immediately below the PHP code, one line that is an HTML meta tag. That HTML meta tag needs to be below the PHP code because the PHP code provides a value the meta tag requires.

Because we have our attention on it, let's mention what can be customized in the meta tag now, before we address the preceding PHP code.

Within the meta tag, you'll see content="0;

The 0 specifies the number of seconds to delay before the download proceeds, 0 seconds in this case. To implement a delay, specify a number greater than 0.

Now, the PHP code:

The customization area is colored purple. There are three variables with values that can be customized. The lines with the variables are colored red.

  1. $URLofDownloadableFile = "/downloads/thedoc.zip"

    Change /downloads/thedoc.zip to the URL of the file to be downloaded when the download page is loaded into a browser. The URL may be an absolute http://… or https://… URL. Or, it may be a relative URL (as is the example).

  2. $LocationOfLogFile = "/downloads/log/log.csv";

    If you wish to maintain a download log file, change /downloads/log/log.csv to the location where the file is to be maintained. The download log file is a CSV file. (The subdirectories for the CSV file must already exist and be writable. The CSV file is created automatically.)

    On the other hand, if you do not wish to maintain a download log file, remove /downloads/log/log.csv. You would then end up with $LocationOfLogFile = "";

  3. $LocationOfCountFile = "/downloads/log/count.txt";

    If you wish to keep a count of the number of downloads, change /downloads/log/count.txt to the location where the counting file is to be maintained. (The subdirectories for the count file must already exist and be writable. The count file is created automatically.) The count file does not maintain a number. Instead, it appends an x into the file every time a file is downloaded. Thus, the file size is the number of downloads.

    (The reason for appending an x to the file with every download is because it is at least twice as fast and also more reliable than (i) reading the file to obtain the last number, (ii) incrementing the number, then (iii) writing the number back to the file. It is more reliable because virtually simultaneous downloads from two or more browsers could cause an error in the count when the last number in the file is read a second time before the first time has a chance to write the incremented number. Getting the count is also easier — simply get the file size instead of opening and reading the file.)

    On the other hand, if you do not wish to have a count of downloads available, remove /downloads/log/count.txt. You would then end up with $LocationOfCountFile = "";

When your customization is complete, it is ready to insert into the HEAD area of the download page source code.

Additional Options

At the download page (with the above-mentioned PHP code installed), two optional features may be published in the BODY area.

Feature One —

Some browsers have a preference setting that allows the user to block redirects. For those, the meta tag will not work — it is, in essence, a redirect tag.

To accomodate those, the web page may contain something like this in its source code.

<p>
If download does not start presently, 
<a href="<?php echo($URLofDownloadableFile) ?>">
tap here.
</a>
</p>

The <?php echo($URLofDownloadableFile) ?> PHP code automatically inserts the URL of the downloadable file into the link. The person can tap on "tap here" and download the file.

Feature Two —

If you wish to publish the download count on the download page, the <?php echo(filesize($LocationOfCountFile)) ?> PHP code automatically prints the number. It may be implemented like this:

<p>
Number of downloads: <?php echo(filesize($LocationOfCountFile)) ?>
</p>

When this system is implemented, your download page initiates the download when the page is loaded into the browser. Optionally, the following can be implemented:

  • Maintain download log file.
  • Update download count file.
  • Publish direct download link.
  • Publish current download count.

Test the page to verify it works as it should, with the options you want. Then it's ready for the public or people you privately direct to the page.

(This article first appeared with an issue of the 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-2024 Will Bontrager Software LLC