...

View Full Version : Detect if amount of time is in minutes?



martynball
06-23-2010, 11:31 AM
I have made a script to detect the amount of bytes roughly transferred per second which it uses to then estimate the amount of time which a file would take to download.

Only problem is I don't know how to figure out if the amount of seconds is in minutes or hours :/ because for some reason there is always 1 dot in front of the number, it seems like if the estimated time is in the tens (min) then an "E" is added :S

Examples:
Estimated Download Time: 1.578
Estimated Download Time: 7.93E



<?php
//Amount of time which page takes to load in microseconds.
$time = microtime();
$time = explode(" ", $time);
$time = $time[0] + $time[1];

//Function which can be used to get the current file size which script is contained, can also
//calculate the amount of bytes which are downloaded based on the page load time and file size.
function file_namesize($fun) {
global $time;
if ($fun == "name") {
$dir = $_SERVER['PHP_SELF'];
$filename = strrchr($dir, "/");
$filename = substr($filename, 1);
return $filename;
unset($dir,$filename);
}
elseif ($fun == "size") {
$name = file_namesize("name");
$filesize = filesize($name);
return $filesize;
unset($name,$filesize);
}
elseif ($fun == "bps") {
$fsize = file_namesize("size");
$bps_f = $fsize / $time;
$bps_f = substr($bps_f, 0, 5);
return $bps_f;
unset($fsize,$bps_f);
}
}

//Display results
echo "Page load time: ".$time."<br/>";
echo "Current Pagesize: ".file_namesize("size")." Bytes.<br/>";
echo "Filename: ".file_namesize("name")."<br/><br/>";
echo "Bytes Per Second (BPS): ".file_namesize("bps");

//Below is the script which calculates how long a file will take to download based on the bytes per second
//downloaded in above script.

//Image location referenced below is the file which the script uses to estimate the estimated download time.
$file1 = "images/Reach_E310_Firefight_Beachhead04.jpg";
//P100619_194537.jpg
//katie edgell2.jpg

//Estimate the download time for selected image.
function est_dlt($file) {
$bps = file_namesize("bps");
$file_size = filesize($file);
$result = $bps / $file_size;
$get_dot = strstr($result, ".");
$count = count($get_dot[0]);
$result = substr($result, 0, 5);

return $result.", ".$count;
}

echo "<br /><br /><br /><img src=\"".$file1."\" width=\"200px\"/><br />";
echo "Image Size: ".filesize($file1)." Bytes - Estimated Download Time: ".est_dlt($file1);
?>

Rowsdower!
06-23-2010, 02:30 PM
I have made a script to detect the amount of bytes roughly transferred per second which it uses to then estimate the amount of time which a file would take to download.

Only problem is I don't know how to figure out if the amount of seconds is in minutes or hours :/ because for some reason there is always 1 dot in front of the number, it seems like if the estimated time is in the tens (min) then an "E" is added :S

Examples:
Estimated Download Time: 1.578
Estimated Download Time: 7.93E



<?php
//Amount of time which page takes to load in microseconds.
$time = microtime();
$time = explode(" ", $time);
$time = $time[0] + $time[1];

//Function which can be used to get the current file size which script is contained, can also
//calculate the amount of bytes which are downloaded based on the page load time and file size.
function file_namesize($fun) {
global $time;
if ($fun == "name") {
$dir = $_SERVER['PHP_SELF'];
$filename = strrchr($dir, "/");
$filename = substr($filename, 1);
return $filename;
unset($dir,$filename);
}
elseif ($fun == "size") {
$name = file_namesize("name");
$filesize = filesize($name);
return $filesize;
unset($name,$filesize);
}
elseif ($fun == "bps") {
$fsize = file_namesize("size");
$bps_f = $fsize / $time;
$bps_f = substr($bps_f, 0, 5);
return $bps_f;
unset($fsize,$bps_f);
}
}

//Display results
echo "Page load time: ".$time."<br/>";
echo "Current Pagesize: ".file_namesize("size")." Bytes.<br/>";
echo "Filename: ".file_namesize("name")."<br/><br/>";
echo "Bytes Per Second (BPS): ".file_namesize("bps");

//Below is the script which calculates how long a file will take to download based on the bytes per second
//downloaded in above script.

//Image location referenced below is the file which the script uses to estimate the estimated download time.
$file1 = "images/Reach_E310_Firefight_Beachhead04.jpg";
//P100619_194537.jpg
//katie edgell2.jpg

//Estimate the download time for selected image.
function est_dlt($file) {
$bps = file_namesize("bps");
$file_size = filesize($file);
$result = $bps / $file_size;
$get_dot = strstr($result, ".");
$count = count($get_dot[0]);
$result = substr($result, 0, 5);

return $result.", ".$count;
}

echo "<br /><br /><br /><img src=\"".$file1."\" width=\"200px\"/><br />";
echo "Image Size: ".filesize($file1)." Bytes - Estimated Download Time: ".est_dlt($file1);
?>


I think that "E" is the mathematical notation for an exponential (base 10) multiplier. So following that logic, I would say that "7.93E" is actually 79.3 seconds, which is 1 minute and 19.3 seconds.

If, for example, it were "7.93E5" it would be 793,000 seconds (7.93 x 10^5).

Anyway, I'm pretty sure this is what it is, but I might be wrong. So check into it. :thumbsup:

martynball
06-24-2010, 03:30 PM
Actually, wouldnt this script be counting the page load time for the actual server as it is running the script. So it would not be detecting the users internet speed would it?

kar2905
06-24-2010, 03:39 PM
Ya, E denotes the exponent. To get the time in minutes, just divide by 60. The quotient would be the minutes and the remainder is seconds

Fou-Lu
06-24-2010, 04:45 PM
Actually, wouldnt this script be counting the page load time for the actual server as it is running the script. So it would not be detecting the users internet speed would it?

Thats correct. If you remove the substring call inside your est_dlt function, you could get a better idea of the time. My guess is that both of those numbers you have posted are actually E-X (that is, E to the negative x) numbers to reflect the $time specified by the server which will be fractions of a second. This is because your resultant has been further divided by the size in question, thus making this smaller than a normal number displayable with the 23 bits available in a float mantissa.

To get the download speed from a client, you will need to connect to them. If you have exec based privileges, you can ping your client, capture the results of the ping and average out the packet size -> transfer ratio assuming that the upload reply is negligable. Alternatively, try establishing a socket connection with the client, and forcing some data down the pipe. Loop this for several records and draw an average.

martynball
06-24-2010, 10:28 PM
Thats correct. If you remove the substring call inside your est_dlt function, you could get a better idea of the time. My guess is that both of those numbers you have posted are actually E-X (that is, E to the negative x) numbers to reflect the $time specified by the server which will be fractions of a second. This is because your resultant has been further divided by the size in question, thus making this smaller than a normal number displayable with the 23 bits available in a float mantissa.

To get the download speed from a client, you will need to connect to them. If you have exec based privileges, you can ping your client, capture the results of the ping and average out the packet size -> transfer ratio assuming that the upload reply is negligable. Alternatively, try establishing a socket connection with the client, and forcing some data down the pipe. Loop this for several records and draw an average.

Don't know of any tutorials or example codes which show how to ping a client by any chance do you?

Fou-Lu
06-24-2010, 10:47 PM
Ping is simply done with either backticks or shell_exec:


$iPingCount = 4;
$iPacketSize = 512;
$cmd = sprintf('ping -c %d -s %d -q %s', $iPingCount, $iPacketSize, $userSuppliedIPAddress);
// Windows server use:
// $cmd = sprintf('ping -n %d -l %d %s', $iPingCount, $iPacketSize, $userSuppliedIPAddress);
$exec = shell_exec($cmd);

$exec will contain a number of lines, the last two are the most important. Depeding on OS depends on results, so you'll need to accommodate the formatting for each. Linux will look something like:


--- 127.0.0.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3998ms
rtt min/avg/max/mdev = 0.047/0.053/0.061/0.009 ms

And Windows something like:


Ping statistics for 127.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms


What matters is the average for each. Some string manipulation should let you get those out (exploding and / or sscanf will do it for you).

martynball
06-24-2010, 11:14 PM
my host has disabled shell_exec. Got an example I can work off using the backticks technique?

Fou-Lu
06-24-2010, 11:20 PM
You can try using $exec = `$cmd`; instead. If thats disabled, another option is to open a socket to the user (which is not something I can actually test, but I can certainly try to write it).

martynball
06-24-2010, 11:24 PM
any chance you could give me a quick example of the other code so that I can read it and try to understand how it works?

also, by using that code you just give me and the replacement it just gives me a blank screen, how do I view the results?

Fou-Lu
06-24-2010, 11:48 PM
print $exec;.
Here is an example of 'pinging' a machine in PHP using fsockopen. You can't actually ping it though; ICMP is not available for PHP to use.
http://php.net/manual/en/function.fsockopen.php#47495

martynball
06-24-2010, 11:55 PM
"print $exec" produces "$cmd". Also, are you saying I cannot ping the client then?

Fou-Lu
06-25-2010, 12:06 AM
Oh wait, what am I thinking. Backticks are only enabled if shell_exec is enabled. Lol, so no goes there.
Look into the socket connection instead.

Oh, and yeah you cannot actually 'ping' a client with PHP and sockets; ICMP traffic is not supported by PHP. You can however simulate a ping by attempting to connect and determining the amount of time it takes to be rejected (hopefully rejected >.<), which is pretty much what the ping does anyway.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum