Grant Grant - 1 year ago 52
PHP Question

How do I track file downloads

I have a website that plays mp3s in a flash player. If a user clicks 'play' the flash player automatically downloads an mp3 and starts playing it.

Is there an easy way to track how many times a particular song clip (or any binary file) has been downloaded?

Is the play link a link to the actual
mp3 file or to some javascript code
that pops up a player?

If the latter, you can easily add your
own logging code in there to track the
number of hits to it.

If the former, you'll need something
that can track the web server log
itself and make that distinction. My
hosting plan comes with webalizer,
which does this nicely.

It's javascript code, so that answers that.

However, it would be nice to know how to track downloads using the other method (without switching hosts).

Answer Source

The funny thing is i wrote a php media gallery for all my music 2 days ago. I had a similar problem. Im using for the player. and the playlis are built via php. all music request go there a script called xfer.php?file=WHATEVER

$filename = base64_url_decode($_REQUEST['file']);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($filename));
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));

//  Put either file counting code here. either a db or static files


readfile($filename);  //and spit the user the file

function base64_url_decode($input) {
    return base64_decode(strtr($input, '-_,', '+/='));

And when you call files use something like

function base64_url_encode($input) {
     return strtr(base64_encode($input), '+/=', '-_,');

If you are using some javascript or a flash player (JW player for example) that requires the actual link to be an mp3 file or whatever, you can append the text "&type=.mp3" so the final linke becomes something like "". That way it looks like it ends with an mp3 extension without affecting the file link.