Link to home
Start Free TrialLog in
Avatar of Aetia
Aetia

asked on

Download time

UNGRENT, 500 points given...

Has enybody got script to show file download time. App should recognize somehow visitors connection speed itself.
Avatar of thecode101
thecode101

Try this:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language = "JavaScript">
// Remove down to "ConnectionSpeed Detection section" if you don't want to use cookies
// If you drop a cookie it can be picked up on return by php or something else
function setCookie(name, value, expire ){
document.cookie = name + "=" + escape(value) + ((expire==null ?
"":(";expires=" + expire.toGMTString())));
// alert('A cookie called '+name+' is now set with value: '+value); //enable to alert user of cookie
}
function getExpireDate(){
var expires = new Date();
expires.setTime((new Date().getTime() + 1000*60*60*24*365));
return expires;
}
// ConnectionSpeed Detection section
var datasize=31468; // Size of data being transferred, in Bytes
var startTime=0;
var endTime=0;
var date=0;
var ctype="";
var textMessage="";
function calcThroughput() {
var diffTimeMilliseconds = endTime - startTime;
var diffTimeSeconds = diffTimeMilliseconds/1000;
var bits = (datasize*8);    // convert Bytes to bits,
var kbits = bits/1024;    // convert bits to kbits
var throughput = kbits/(diffTimeSeconds*100/100);
throughput = throughput * .93; // account for IP packet header overhead - averages about 7%
setCookie("MediaThroughput", throughput, getExpireDate()); // Remove to not use cookie
if (throughput < 600){ctype="medium.html";}
if (throughput < 560){ctype="low.html";}
if (throughput > 600){ctype="high.html";}
textMessage = "Bandwidth: <B>" + ctype + "</B><br>time to load: (sec): <B>" + diffTimeSeconds + "</B><BR>kbits loaded:<B> " + kbits + "</B><BR>Throughput (kbps): <B>" + throughput +"</B>"
var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
document.location = sPath + "?speed=" + bits;
}
</script>
</head>

<body>
<?php
if (!$speed)
{
echo "<script language = \"Javascript\">
date = new Date();
endTime=date.getTime();
calcThroughput();
</script>";
}

$size = filesize ("path/to/filename");
$speed = floatval($speed);
$size = floatval($size);
$downloadTime = ceil($size / $speed);
echo $downloadTime;
?>

</body>
</html>

Code borrowed and modified from:
http://www.gidforums.com/t-1285.html
http://twebman.lunarpages.com/js/BandwidthRedirect.htm
ASKER CERTIFIED SOLUTION
Avatar of thecode101
thecode101

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aetia

ASKER

Thank you...

I havent tested it... But I will give you your points... :D
Avatar of Aetia

ASKER

I used your script In my PHP it seems to function.

However the result of the calculation of the downloading time is theoretical and it represents a minimal time (it takes the speed of connection and it doesn't know the speed of the distant server and the different roads that data can follow).