How can the response headers be parsed effectively to retrieve specific file information when accessing a remote server using PHP?
When accessing a remote server using PHP, you can parse the response headers to retrieve specific file information by using the get_headers() function. This function sends a HTTP request to the specified URL and returns an array of headers. You can then extract the information you need from the headers array, such as file size, content type, last modified date, etc.
$url = 'https://www.example.com/file.txt';
$headers = get_headers($url, 1);
$fileSize = $headers['Content-Length'];
$contentType = $headers['Content-Type'];
$lastModified = $headers['Last-Modified'];
echo "File Size: " . $fileSize . "<br>";
echo "Content Type: " . $contentType . "<br>";
echo "Last Modified: " . $lastModified . "<br>";