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:
-
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.
-
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.
-
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. -
In the source code, at the
$ForceFileDownload = "no";
line, replaceno
withyes
if you wish to force download the file. Otherwise, leaveno
as the value. -
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