How can the filemtime function be unreliable when accessing files via URL wrappers in PHP?

When accessing files via URL wrappers in PHP, the filemtime function can be unreliable because it may not work as expected due to the limitations of URL wrappers. To solve this issue, you can use cURL to fetch the file and then get the modification time using the CURLOPT_FILETIME option, which ensures that the correct modification time of the file is retrieved.

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

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

curl_exec($ch);

$filemtime = curl_getinfo($ch, CURLINFO_FILETIME);

if($filemtime != -1) {
    echo "Last modified: " . date("Y-m-d H:i:s", $filemtime);
} else {
    echo "File modification time not available.";
}

curl_close($ch);