What are the potential pitfalls of using file_exists with remote files and what alternative approach can be used?

Using file_exists with remote files can be slow and inefficient as it requires PHP to make a network request to check the existence of the file. Instead, a more efficient approach is to use functions like fopen or curl to check for the existence of remote files. This allows for more control over the request and can provide additional information about the file.

function remote_file_exists($url) {
    $file = @fopen($url, 'r');
    if ($file) {
        fclose($file);
        return true;
    } else {
        return false;
    }
}

$url = 'http://www.example.com/remote-file.txt';
if (remote_file_exists($url)) {
    echo 'Remote file exists!';
} else {
    echo 'Remote file does not exist.';
}