How can the HEAD method be utilized to improve the efficiency of checking file modification time in PHP?

When checking the modification time of a file in PHP, using the HEAD method can improve efficiency by requesting only the headers of the file instead of the entire file content. This reduces unnecessary data transfer and processing time, making the operation faster and more efficient.

$filename = 'example.txt';

// Initialize cURL session
$ch = curl_init($filename);

// Set cURL options to perform a HEAD request
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the cURL request
curl_exec($ch);

// Get the last modified time from the headers
$last_modified = curl_getinfo($ch, CURLINFO_FILETIME);

// Close the cURL session
curl_close($ch);

// Output the last modified time
echo "Last modified time: " . date('Y-m-d H:i:s', $last_modified);