How can filemtime and file_exists functions behave differently when accessing files via URL wrappers in PHP?

When accessing files via URL wrappers in PHP, the filemtime and file_exists functions may behave differently because they might not work with URLs. To solve this issue, you can use the curl_getinfo function to get the last modified time of a file accessed via URL and check if the file exists by checking the HTTP response code.

$url = 'http://example.com/file.txt';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);

if(curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200) {
    $lastModified = curl_getinfo($ch, CURLINFO_FILETIME);
    if($lastModified != -1) {
        echo "Last modified time: " . date("Y-m-d H:i:s", $lastModified);
    } else {
        echo "Last modified time not available.";
    }
} else {
    echo "File does not exist.";
}

curl_close($ch);