How can PHP be used to compare and synchronize local and remote files, taking into account file modification dates?
To compare and synchronize local and remote files, we can use PHP to retrieve the modification dates of both files and then compare them. If the modification dates differ, we can update the file with the latest version. This ensures that both files are in sync and up to date.
$localFile = 'local_file.txt';
$remoteFile = 'http://example.com/remote_file.txt';
$localModTime = filemtime($localFile);
$remoteModTime = strtotime(get_headers($remoteFile, 1)['Last-Modified']);
if ($localModTime < $remoteModTime) {
file_put_contents($localFile, file_get_contents($remoteFile));
echo "Local file updated with remote version.";
} elseif ($localModTime > $remoteModTime) {
file_put_contents($remoteFile, file_get_contents($localFile));
echo "Remote file updated with local version.";
} else {
echo "Files are already in sync.";
}