How to detect internet speed in PHP?

This might not be completely what you're looking for (read the bold part), but I doubt if anything else is possible.

This script sends 512 KB of HTML comments to your client. Parsing that HTML may add to the total transfer time, so don't take this as your raw download speed.

Quoted from: PHP Speed test

Source is here:

http://jan.moesen.nu/code/php/speedtest/index.php?source=1

Hope that helps.


For example by timing AJAX request on client side. That way you can figure approximate download speed, but not upload. For uploading, sending large AJAX POST request can handle it.

With jQuery and $.ajax it's pretty trivial to do.


<?php
$kb=1024;
echo "streaming $kb Kb...<!-";
flush();
$time = explode(" ",microtime());
$start = $time[0] + $time[1];
for($x=0;$x<$kb;$x++){
    echo str_pad('', 1024, '.');
    flush();
}
$time = explode(" ",microtime());
$finish = $time[0] + $time[1];
$deltat = $finish - $start;
echo "-> Test finished in $deltat seconds. Your speed is ". round($kb / $deltat, 3)."Kb/s";
?>