How can PHP be used to measure upload and download speeds from a remote server?

To measure upload and download speeds from a remote server using PHP, you can create a script that sends a file to the server and measures the time it takes to upload and download the file. You can calculate the speed by dividing the file size by the time taken. This can be useful for testing network performance or monitoring connection speeds.

<?php
$remoteFile = 'http://example.com/file.zip';
$localFile = 'file.zip';

// Measure download speed
$startDownload = microtime(true);
file_put_contents($localFile, file_get_contents($remoteFile));
$endDownload = microtime(true);
$downloadSpeed = filesize($localFile) / ($endDownload - $startDownload);

// Measure upload speed
$startUpload = microtime(true);
file_get_contents($remoteFile, false, stream_context_create([
    'http' => [
        'method' => 'PUT',
        'header' => 'Content-type: application/octet-stream',
        'content' => file_get_contents($localFile)
    ]
]));
$endUpload = microtime(true);
$uploadSpeed = filesize($localFile) / ($endUpload - $startUpload);

echo 'Download speed: ' . round($downloadSpeed, 2) . ' bytes/sec';
echo 'Upload speed: ' . round($uploadSpeed, 2) . ' bytes/sec';
?>