?
Solved

recording download stats with PHP (Download Manager)

Posted on 2009-05-01
7
Medium Priority
?
281 Views
Last Modified: 2013-12-12
Hi,

I have a PHP script that allows users to download a zip-file. I need to build a download manager or have a way of recording some stats such as complete downloads, partial downloads, etc. Is there a php library that anyone could recommend me for doing this? If I have to write this myself, could I get some hints as what this involves?

Thanks
0
Comment
Question by:dmehran
  • 3
  • 3
6 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24281290
I don't know of a library, but it might be pretty easy to write the script.  You would keep track of this in a data base table.  The general logic would go like this.

The data base table has fields dl_id (AUTO_INCREMENT), dl_user_id, dl_start (DATETIME), dl_end (DATETIME), dl_filename

1. Add a record to the table with the user-id, the current timestamp and the name of the file that is to be downloaded.
2. Start the download
3. Upon completion, update the record you just added to fill in the timestamp when the download completed.

Then you can query that table to find any rows with an empty "dl_end" and you'll know of incomplete downloads.  You can also find which users have downloaded which files, etc.

HTH, ~Ray
0
 

Author Comment

by:dmehran
ID: 24298942
Ray,

Thanks for replying to my question. I am aware of database solutions to record the data. The part I am having difficulty with is detecting when a download starts and more importantly when download completes. How is that done? I can implement a front end solution in Flash to monitor the downloaded data packets vs the total file size; as a matter of fact Flash does have a FTP component with provided methods that I can use to monitor file upload/download. However we are looking for a server side solution. The company I work for doesn't rely too much on front-end solutions such as flash for a remote chance that a user may not have flash installed. A server solution at this point to detect the start and completion of a file download is what I am looking for at this point.

Thanks
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 24299959
See if this makes sense.  It should mimic what you would get if you had this sort of thing in the HTML...

<a href="http://domain.com/img/rds_2474.jpg">DL</a>

... but it adds the data base wrapper to the process.  I don't have the means to test it, so beware of my typos!

Best regards, ~Ray
<?php // RAY_force_download.php
error_reporting(E_ALL);
session_start();
 
 
// TEST DATA FOR GET STRING: ?f=http://v1.ativo.com/img_campanhas/volks_2008/rds_2474.jpg&u=ABC
// THE DB TABLE NAMED dl_table HAS FIELDS
//   dl_id (AUTO_INCREMENT),
//   dl_user_id,
//   dl_start (DATETIME),
//   dl_end (DATETIME),
//   dl_filename
 
 
// A FUNCTION TO FORCE A FILE DOWNLOAD
function force_download($filename)
{
   $basename = basename($filename);
   $filedata = file_get_contents($filename);
 
   if ($filedata)
   {
      header("Content-Type: application/force-download");
      header("Content-Disposition: attachment; filename=\"$basename\"");
      header("Content-length: ".(string)(strlen($filedata)));
      header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
      header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
// MAYBE OMIT THIS LINE IF YOU DETECT IE6      header("Cache-Control: no-cache, must-revalidate");
      header("Pragma: no-cache");
      flush();
 
      echo $filedata;
      flush();
   }
}
 
 
// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";
 
// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>NO DB CONNECTION: ";
   echo "<br/> $errmsg <br/>";
}
 
// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>NO DB SELECTION: ";
   echo "<br/> $errmsg <br/>";
   die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES
 
 
// ESCAPING A DATA FIELD FOR USE IN MYSQL QUERIES
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-real-escape-string.php
// FILE NAME AND USER ID STRING
$f = mysql_real_escape_string($_GET["f"]);
$u = musql_real_escape_string($_GET["u"]);
$s = date('c');
 
// INSERTING THE STARTING DATA INTO THE TABLE
$sql = "INSERT INTO dl_table (dl_user_id, dl_start, dl_filename) VALUES (\"$u\", \"$s\", \"$f\")";
$res = mysql_query($sql);
 
// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>QUERY FAIL: ";
   echo "<br/>$sql <br/>";
   die($errmsg);
} // IF WE GET THIS FAR, THE QUERY SUCCEEDED
 
// GET THE AUTO_INCREMENT ID OF THE RECORD JUST INSERTED
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-insert-id.php
$dl_id  = mysql_insert_id();
 
// START THE DOWNLOAD
force_download($_GET["f"]);
 
// WHEN DOWNLOAD COMPLETES
$t   = date('c');
$sql = "UPDATE dl_table SET dl_end = \"$t\" WHERE dl_id = $dl_id LIMIT 1";
$res = mysql_query($sql);
 
// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>QUERY FAIL: ";
   echo "<br/>$sql <br/>";
   die($errmsg);
} // IF WE GET THIS FAR, THE QUERY SUCCEEDED
 
// COMPUTE ELAPSED TIME
$lapse = date('H:i:s', strtotime($t) - strtotime($s));
echo "<br/> DL TIME FOR $f IS $lapse \n";
?>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24299974
The reason for the session_start() statement is that I was thinking of adding a key to the GET string and storing it in the session, then testing it in this script to see if the GET key matched the SESSION key.  It's a minor security step, probably not needed at this point in the proof of concept stage.
0
 

Author Comment

by:dmehran
ID: 24307685
Thanks!!! I'll try this and will let you know
0
 

Author Closing Comment

by:dmehran
ID: 31576942
With some modifications I got this work. Had to modify the solution to tailor it to my application. Thanks for the solution.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question