Simple PHP Download Counter Script

AffiliStore, My Websites, PHP, Web Design Add comments

Here is a simple way to create a download counter script using PHP. It simply counts each click on the download link and stores the result in a writable text file.

First upload the file you wish users to download to your server. Then create a text file and name it “counter.txt“. Open the text file and add a 0, save and upload to your server and set the text file permissions to 777.

Now create a PHP file named “countdownloads.php” and add the following code:

<?php
$myFile = "counter.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);

$theData = $theData + 1;

$myFile = "counter.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $theData);
fclose($fh);

header("Location: download.zip");
?>

Now add the following code to your HTML page that will display the link to your download:

<p><a href="countdownloads.php">Download File</a><br />
<?php
$myFile = "counter.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
echo $theData;
fclose($fh);
?> Downloads<br />
since Feb 2008</p>

Upload these files to the same directory on your server and that should do the trick. I’m using this script on AffiliStore to count the downloads of the software.

Leave a Reply

Close
E-mail It