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!

File Download Counter

When you offer a downloadable file — PDF, MP3, image, etc — you may wish to know how many people actually download the file.

Click counting software is one way to get that information. This article contains another way for individual files. (If you need to track a large number of different file downloads, use Download Handler).

The method this article provides is a PHP script named File Download Counter.

Install the script (source code and instructions further below). Then link to the script instead of directly to the downloadable file.

File Download Counter logs the click and tells the browser to do the download.

The click log includes the date and time and some other information. There is one line per download. (To count how many downloads have occurred, read how many lines there are in the log file.)

There are two types of downloads:

  1. Browser's Choice — The browser can choose to download the file to the hard drive or to download the file and display it in a browser window.

    If it is a web page or plain text file, the browser is most likely to display the file in its own window. Some file types, like PDF or MP3, might display in the browser window or download to the hard drive, depending on browser preferences and settings.

  2. Force Download — The browser is told explicitly to download the file to the hard drive regardless the file type, even HTML and plain text files.

    In a very few cases, the browser may refuse:

    • There is insufficient room on the hard drive to store the file.

    • No area on the hard drive has been specified as the location to save downloaded files.

    • Browser preferences or computer/device security settings prevent the download.

    It is entirely up to the browser and the computer/device whether or not to download a file you're forcing. However, except for the few browsers when the above may apply, the browser is virtually certain to do the download.

When you customize the File Download Counter source code, you can specify whether to let the browser choose the destination of the download (hard drive or browser window) or to explicitly direct the browser to store the file on the hard drive.

The Source Code

Here is the source code code for the File Download Counter PHP script. Notes follow.

<?php
/*
   File Download Counter
   Version 1.0
   August 25, 2019

   Will Bontrager Software LLC
   https://www.willmaster.com/
*/

/////////////////////////////////////////
// Customizations (3 places to customize)

// Place 1:
// Specify the location of the downloadable file 
//    (location relative to the document root directory).

$LocationOfDownloadableFile = "/directory/file.pdf";


// Place 2:
// Specify "yes" or "no" to force download. If "no" 
//    (or blank or anything other than "yes") the 
//    browser will decide whether to download or 
//    load into current browser window.)

$ForceFileDownload = "no";


// Place 3:
// Specify the location of the log file in a subdirectory. 
//    The directory must be writable, which might require 
//    777 permissions, depending on how PHP is configured.

$LocationOfLogFile = "/directory/log.txt";

// End of customization section
/////////////////////////////////////////

file_put_contents("{$_SERVER['DOCUMENT_ROOT']}$LocationOfLogFile",date('r')."\t".$_SERVER['REMOTE_ADDR']."\t$LocationOfDownloadableFile\t".$_SERVER['HTTP_USER_AGENT']."\n",FILE_APPEND);
if( trim(strtolower($ForceFileDownload)) == 'yes' )
{
    $filename = array_pop( explode('/',$LocationOfDownloadableFile) );
    header('Content-Type:application/octet-stream');
    header("Content-Disposition:attachment; filename=\"$filename\"");
    readfile("{$_SERVER['DOCUMENT_ROOT']}$LocationOfDownloadableFile");
}
else { header("Location: $LocationOfDownloadableFile"); }
exit;
?>

Notes:

There are three places for customizing the software, all clearly marked with instructions in the source code.

  1. Specify the location of the downloadable file, relative to the document root directory. If the file were at https://example.com/directory/file.pdf, then the location relative to the document root directory would be /directory/file.pdf.

    In the source code, at the $LocationOfDownloadableFile = "/directory/file.pdf"; line, replace /directory/file.pdf with the correct downloadable file location.

  2. In the source code, at the $ForceFileDownload = "no"; line, replace no with yes if you wish to force download the file. Otherwise, leave no as the value.

  3. Specify the location of the click logging file, relative to the document root directory. If the file were at https://example.com/directory/log.txt, then the location relative to the document root directory would be /directory/log.txt.

    In the source code, at the $LocationOfLogFile = "/directory/log.txt"; line, replace /directory/log.txt with the correct downloadable file location.

    The directory where the log file is to be maintained must be writable, which might require 777 permissions, depending on how PHP is configured on your server.

Installation and Use

When the edits to the PHP source code have been completed, save the File Download Counter file as filedownload.php (or other *.php file name) and upload it to your server. Make a note of its URL.

To use File Download Counter, link to it with its URL. Example link code:

<a href="https://example.com/filedownload.php">
Tap to download the special file.
</a>

Replace https://example.com/filedownload.php with the URL to your File Download Counter installation and you are good to go.

Or, at least you should be good to go. Do check the link to verify it works as it should.

When it works as it should, tapping the link causes a click log file entry to be made and the file to be downloaded.

(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