how to track number of downloads for a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

how to track number of downloads for a file

Post by eshban »

hi,

i need some help , how can i track the downloaded files. The files are hosted on my server. I need some php code to track number of downloads per file.

thnaks
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: how to track number of downloads for a file

Post by andym01480 »

I swap direct links to the files with someothing like download.php?file=888
Then have a database table with columns for filename, file_id and no of downloads

The download.php script updates the number of downloads by 1 and then sends headers and readfile("filename")

Job done
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: how to track number of downloads for a file

Post by Jonah Bron »

Or, simpler method:
count.txt:

Code: Select all

0
download.php:

Code: Select all

<?php
$file_to_download = 'file.exe';
$count = file_get_contents('count.txt');
$count++;
fwrite(fopen('count.txt, 'w'), $count);
header('Location: '. $file_to_download);
?>
Just link to "download.php", instead of "file.exe"
The file called "count.txt" contains the number of times the file has been downloaded.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: how to track number of downloads for a file

Post by andym01480 »

Simpler if only one file to download!
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Re: how to track number of downloads for a file

Post by eshban »

but how can i know that user has successfully downloaded the file or not?
Post Reply