How can PHP be used to test if a server is available and redirect image paths to a secondary server if the primary server is down?

To test if a server is available and redirect image paths to a secondary server if the primary server is down, you can use PHP to make a simple HTTP request to the primary server and check the response status code. If the server is down, you can then dynamically update the image paths to point to the secondary server instead.

$primaryServer = 'http://primary-server.com/';
$secondaryServer = 'http://secondary-server.com/';

// Check if primary server is available
$ch = curl_init($primaryServer);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// Redirect image paths to secondary server if primary server is down
if ($httpCode == 200) {
    $imagePath = 'http://primary-server.com/images/image.jpg';
} else {
    $imagePath = 'http://secondary-server.com/images/image.jpg';
}

echo '<img src="' . $imagePath . '" alt="Image">';