Solved

Download time

Posted on 2004-08-12
4
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Node.js 11 72
How do I speed up this PDO query 4 37
Code planning methods/tools? 5 51
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given PHP 5 34
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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

738 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