Troy R Troy R - 3 months ago 6x
PHP Question

Does @get_headers download the full file or just the metadata?

I have download files on my website that are using @get_headers to check if the files exist and to retrieve the Content-Length (file size) metadata. I recently moved by download files to my CDN because I noticed a 50% speed increase in downloads but after the move I've noticed a huge increase in my CDN usage from 5GB to 120GB+ per month after moving the files.

I was wondering does @get_headers download the full file to retrieve the metadata or does it grab the metadata without downloading the full file?

If @get_headers does download the full file to retrieve metadata is there another solution?



It uses a GET, which is what you're calling a "full file". What you want to do is use a HEAD request. This is documented in a comment on the PHP page for the function:

// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
        'http' => array(
            'method' => 'HEAD'
$headers = get_headers('');