Solved

Download time

Posted on 2004-08-12
4
253 Views
Last Modified: 2008-03-06
UNGRENT, 500 points given...

Has enybody got script to show file download time. App should recognize somehow visitors connection speed itself.
0
Comment
Question by:Aetia
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:thecode101
ID: 11784238
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
0
 
LVL 3

Accepted Solution

by:
thecode101 earned 500 total points
ID: 11794476
The download time is returned in seconds. If you have any more questions or if this does not solve your question let me know.
0
 

Author Comment

by:Aetia
ID: 11795521
Thank you...

I havent tested it... But I will give you your points... :D
0
 

Author Comment

by:Aetia
ID: 11800966
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).
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

832 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