How can the Last Modified header be utilized in PHP to compare file modification dates between local and remote files?

When comparing file modification dates between local and remote files in PHP, the Last-Modified header can be utilized to determine the last time a file was modified on the server. By retrieving this header from the remote file and comparing it with the local file's modification date, you can determine if the remote file has been updated.

$localFilePath = 'local_file.txt';
$remoteFileUrl = 'http://www.example.com/remote_file.txt';

// Get the Last-Modified header from the remote file
$ch = curl_init($remoteFileUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$lastModified = curl_getinfo($ch, CURLINFO_FILETIME);
curl_close($ch);

// Get the local file's modification date
$localLastModified = filemtime($localFilePath);

// Compare the modification dates
if ($lastModified > $localLastModified) {
    // Remote file has been updated, do something
} else {
    // Remote file has not been updated
}