What alternative function can be used instead of file_exists for checking the existence of remote files in PHP?

When checking the existence of remote files in PHP, the file_exists function may not work reliably due to limitations in handling remote file paths. An alternative function that can be used is get_headers, which sends a HEAD request to the remote file URL and retrieves the response headers. By checking if the response code is 200, we can determine if the file exists remotely.

function remote_file_exists($url) {
    $headers = get_headers($url);
    return strpos($headers[0], '200') !== false;
}

// Example usage
$url = 'https://www.example.com/example.jpg';
if (remote_file_exists($url)) {
    echo 'Remote file exists!';
} else {
    echo 'Remote file does not exist.';
}