Make your own Download SpeedTest in php code

To convert the download speed from KB/s to MB/s, you can divide the speed value by 1024 (since there are 1024 KB in 1 MB). Here's how you can modify your PHP code to display the download speed in megabytes per second (MB/s):

$start_time = microtime(true);

// URL of the file to download
$url = '';

// Initialize curl session
$ch = curl_init($url);

// Set curl options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// Execute curl request and get the content length
$content = curl_exec($ch);
$content_length = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

// Calculate download time and speed
$end_time = microtime(true);
$download_time = ($end_time - $start_time);
$download_speed_kb = ($content_length / $download_time) / 1024; // Download speed in KB/s
$download_speed_mb = $download_speed_kb / 1024; // Convert to MB/s

// Print the download speed in MB/s
echo "Download speed: " . round($download_speed_mb, 2) . " MB/s";

// Close the curl session

In this code, we first calculate the download speed in KB/s, and then we further divide it by 1024 to get the download speed in MB/s. The result is rounded to two decimal places before printing it.

Related Articles

- All From ChatGPT