How can PHP be used to retrieve file headers from a URL when the file is not on the same server?
When trying to retrieve file headers from a URL that is not on the same server, you can use PHP's cURL library to make a request to the remote URL and fetch the headers. This allows you to access information such as the content type, content length, and other metadata about the file.
<?php
$url = 'https://example.com/file.pdf';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$contentLength = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
echo "HTTP Code: $httpCode\n";
echo "Content Type: $contentType\n";
echo "Content Length: $contentLength\n";
?>