Hi,

I'm trying to develop a PHP script which will calculate the internet connection speed of the users. In order to achieve this, the script will perform 5 basic tasks :

1) Fetch the time and store the value in a variable (say: $start_time)
2) Transfer a chunk of data (maybe a image or some other data file) to the user's PC which will be of approx. 250KB.
3) When the download finishes, fetch the time and store the value in a variable (say: $end_time)
4) Calculate the difference between the two times. ($end_time - $start_time) and store the output in another variable (say: $time_taken)
5) Do the appropriate maths to get the speed in KB/s. [I'm not elaborating this since its not necessary right now).


Now, I'm having trouble with step 2 only. In other words, I don't know how to make my script do this step. I want a chunk of data to be transferred to the user's PC. Can anyone tell me which PHP function will be used here?

Regards.

Dani AI

Generated

: the high-level idea is fine, but the timing has to run in the browser. PHP executes on the server, so server-side moves (for example 's move_uploaded_file approach) will not measure a client download. 's short answer is correct: perform the download from client-side JavaScript, measure elapsed time with a high-resolution timer, then POST the measured result back to a PHP endpoint for logging or display.

Example client-side measurement (uses performance.now() for better resolution than Date()):

// client-side: measure download time
const url = '/speed-test.bin?cb=' + Date.now(); // cache-bust
const sizeBytes = 1048576; // known file size in bytes (1 MB)
const t0 = performance.now();
fetch(url, {cache: 'no-store'}).then(r => r.arrayBuffer()).then(() => {
  const seconds = (performance.now() - t0) / 1000;
  const kBps = (sizeBytes / 1024) / seconds; // kB/s
  fetch('/save-speed.php', {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({kBps: kBps})
  });
});

Server-side notes: the test file must be served with an accurate Content-Length and without gzip/transfer compression on that route (otherwise the transmitted byte count and Content-Length differ). A simple PHP stream for the test file would call ini_set('zlib.output_compression',0), set appropriate headers and readfile(). For reliable results, disable caching, use a sufficiently large file for fast links (1MB+), run multiple trials and average, and be aware that proxies, CDNs or mobile networks can distort measurements.

Recommended Answers

All 3 Replies

You can use

$start_time = date():
$file_valid = move_uploaded_file($tmpname,PATH.$file_name);
if($file_valid){
    $end_time = date();
}
$time_taken = $end_time-$start_time;
//calculate the Data Transfer Rate into Kb/s or etc.

I hope this will help you!

: Won't this operation take place within the server itself without any actual transfer of file happening between the server and the User's PC because I think this function is used for moving a file within the same server... or am I wrong?

You need javascript or Ajax my friend ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.