Solved

Download time

Posted on 2004-08-12
4
262 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysqli_connect error on wamp but not on remote server 8 37
Help with PHP 13 27
Test if unique ID is in log file 5 21
Undefined variable problem 5 22
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

860 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