Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine actual download time for a specific file 1

Status
Not open for further replies.

awaresoft

Programmer
Feb 16, 2002
373
DE
I would like to measure and track how long it takes users to download specific files from my site. I'm not looking for an estimate, I would like to record actual download times.

I'm wondering if there's some way I can return the contents of the file being downloaded in my script by setting file headers and capturing before and after times. Or by having a page that saves start time in a database, triggers the download, and then redirects to a page that lookups start time and does the total time calculation. Or if there's someway I can use PHP sessions to help me with this.

Also wondering if I have to remove ob_start() output caching if I'm trying to time actual download times.

Any suggestions on how I can do this via PHP code or PHP code in conjunction with clientside Javascript?

Malcolm
 
You can do it entirely from within PHP. You simply must make PHP the application through which you download files.

For example, this script records the amount of time it takes to send an image file, blowout.jpg:

Code:
<?php
header ('Content-type: image/jpeg');

$fh = fopen ('blowout.jpg', 'rb');

$start = microtime (TRUE);
fpassthru($fh);
$end = microtime (TRUE);
$time = (string) $end - $start;
error_log ($time . "\n", 3, 'test_filetime.log');

fclose($fh);

?>

There are "gotchas" with this method, however. PHP has a maximum runtime, set by the php.ini setting max_execution_time. If the file will take longer than that to run, you'll likely have incomplete data. Also, by putting PHP in the way, you'll be using more server resources than if you simply served the files out directly from thw web server.

As an aside, you could possibly configure your web server to log the amount of time it takes to download a file. Then just parse the logs.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
sleipnir214,

Thanks for your response. That's exactly what I had in mind about passing content through my application so that I could time download time. Using your technique would I have to set a header for content type or does the fpassthru() function provide this functionality?

I understand your point about using server resources efficiently and how your technique would time out with large downloads due to script execution times exceeding their max alloted time.

I'm also wondering if there's a way I can trigger the download followed by an auto-redirect on download completion or handle the timing on the client side via Javascript and then pass the time back via a cookie or HTTP GET?

Any thoughts? I've been googling this concept all morning and have been surprised by the lack of people looking for this type of information.

Thanks again for your help and for your example.
Malcolm
 
You will have to provide the header. All fpassthru() does is stream the file.

I doubt you'll be able to force a redirect after the download. Or if you do, it will be very browser-specific.

Again, what web server are you using? With Apache, it's trivial to get Apache to log the download times for files. Then you might either use a script to parse the files and populate your database periodically, or write a script that will run constantly, watching your logfile and updating the database in realtime.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Sleipnir214,

Thanks for your help, your example, and for pointing out the use of Apache as we way to log download times. I'm using a 3rd party hosted Linux server that allows personal control over my .htaccess file and perhaps other Apache configuration options(?).

I'm an escapee from the Windows world trying to come up to speed on Linux, Apache, MySQL and PHP. So far I've been blown away with the power of this platform.

Any tips on where to start reading with the goal of learning about Apache based logging?

Malcolm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top