Tracking File Download bandwidth

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
myitanalyst
Forum Newbie
Posts: 24
Joined: Wed Dec 14, 2005 4:31 pm

Tracking File Download bandwidth

Post by myitanalyst »

I have a site on a shared server or more accurately a reseller server which is basically shared.

I have podcasts that folks will be downloading to view. Is there anyway to utilize PHP to track the bandwidth used up from the downloads of these files?

You could think of this as hosting and I could simply let the hosting account track the bandwidth used, BUT I have multiple clients under one account and need to track the bandwidth used based on the files downloaded. I know which ones belong to which customer, but I need to be able to let the customer know the bandwidth used since their fee will depend upon this.

Thanks in advance for your time and assistance.

Greg
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

create an intermediate page you could, dump file data into a database it would.

ex:
Main page

Code: Select all

echo "<a href=\"intermediate.php?fid=1\">Some Link Text</a>";
intermediate page

Code: Select all

switch($_GET['fid'])
{
   case 1:
   $filename = "somefile.avi";
   break;
}
// get info for $filename and add size to db

$filesize = filesize($filename);
mysql_query("update myTable set filesize = $filesize + filesize")
   or die(mysql_error())
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

With that you get the problems if a user stops the download 10% into it, you are reporting it wrong, I would say turn on implicit output buffer flushing and output 1024 bytes at a time, updating the database every time. This will definitely hit the database harder so you might want to only update it every 50kb or so, but this way when the user cancels it the client is being billed for bandwidth he/she did not infact use.
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post by trukfixer »

you could just increment a value until it output stops an dthen insert to DB, hwoever , again, you're making php work harder than it has to - I'd side with Burrito on this one..if it is a concern that a user might repeatedly start downloads and then stop them , set a check for teh same IP to instantiate additional downloads of teh same file within a set time frame (say if a user tries to down load the same file a second time in, say 2 minutes or even 30 minutes , dont even report it, dont start the download, dont update the database, and just redirect the user to a "you've already downloaded fileX" )

so I would go with the least intrusive , and fastest delivery method (imagine if you get hit with 100,000 requests a day where the processing necessary to break a file into chunks and the check if it has been sent and then update a counter and write to database, as opposed to simply determine file size and record it to DB .. on average, it's more likely the download *will* complete than to have it interrupted, so teh bandwidth usage would be accurate enough for the purpose. so the extra CPU is only going to load down teh server, potentially to teh point his hosting account gets "shut off" , or the server crashes..)
myitanalyst
Forum Newbie
Posts: 24
Joined: Wed Dec 14, 2005 4:31 pm

Post by myitanalyst »

Burrito wrote:create an intermediate page you could, dump file data into a database it would.
So after doing the above would I then redirect them to the MP3 file being requested?
myitanalyst
Forum Newbie
Posts: 24
Joined: Wed Dec 14, 2005 4:31 pm

Post by myitanalyst »

jshpro2 wrote:With that you get the problems if a user stops the download 10% into it, you are reporting it wrong, I would say turn on implicit output buffer flushing and output 1024 bytes at a time, updating the database every time. This will definitely hit the database harder so you might want to only update it every 50kb or so, but this way when the user cancels it the client is being billed for bandwidth he/she did not infact use.
Ok... I am new to PHP... so how do I do this? This idea sounds like it may be workable, but I could make it update based on larger chunks instead of something small.

Right now I understand the concept, but don't know how to implement.

What does the buffering? how do I make this happen?

Thanks for the help.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

myitanalyst wrote:So after doing the above would I then redirect them to the MP3 file being requested?
if you decide to go my route, you could send the file on the same page that you're inserting the info to the db and just use header()'s to send the file.
Post Reply