I have developed a download / upload script that also counts the downloads of each downloaded file.

For statistics i currently got total files and total overall downloads but would like to extend this so it can say how many downloads there has been in a week, month, year.

I know i would need to probably setup a cron job to reset those stats each month but my dilema is what is the best way and most efficent way of creating such a script that i could implement with my own?

I did a Google search to see if i could find a tutorial to see how other developers do such a task but could not find anything.

If anyone could recommend the best way for me to do this please reply. My stores all the data about the file in a MySql database and files are stored in a folder not MySQL.

Thank you

6 Years
Discussion Span
Last Post by phplover

There are several free stats apps out there that you can download and install. Awstats is a good place to start.



I don't mean that, it's something i want to add to the script, like There was 20 downloads this week, 150 downloads this month.



Assuming that the downloads are routed through a PHP script, it should be simple enough to record user download data. The devil is in the database and how you store this information. Here's my recommendation:
You only need one table to store download information along with hit counts. I recommend three columns for tracking download numbers: A total downloads column, a weekly downloads column, and a monthly downloads column. When a user requests a file download, simply increment all three fields for that file by one.
You stats are simply a MySQL query away:

//Get total downloads
mysql_query("SELECT downloads FROM files WHERE id='".$id."'");

//Get weekly downloads
mysql_query("SELECT weeklyDownloads FROM files WHERE id='".$id."'");

//Get monthly downloads
mysql_query("SELECT monthlyDownloads FROM files WHERE id='".$id."'");

*Depending on what you name your columns

You will need CRON jobs to run once every Sunday and once at the end of every month. The weekly script should contain something along the lines of:

//Set all the files to a 0 weekly download count for the new week
mysql_query("UPDATE files SET weeklyDownloads=0");

As you can assume the monthly CRON will look something like this:

//Set all the files to a 0 monthly download count for the new month
mysql_query("UPDATE files SET monthlyDownloads=0");

**These all assume that you have already connected to MySQL and a DB, and perform the necessary mysql_fetch_array() 's after if the query requires it.


Votes + Comments
Fantastic post made in helping me :)

Thanks PhpMyCoder, Superb example and very simple now you have told me the logic, thank you very much :)

added to your reputation, thanks


Edited by phplover: n/a


Thanks for your kindess :)

I know how to code it thanks, just could not think of the logic behind it.


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.