SOURCES: PHP official documentation - "header" function
There's a function in PHP called header that you would like to use to set headers, before the actual page loads:
Here's the skeleton of the function:
void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
Explanation:
Parameters:
The header string.
There are two special-case header calls. The first is a header that
starts with the string "HTTP/" (case is not significant), which will
be used to figure out the HTTP status code to send. For example, if
you have configured Apache to use a PHP script to handle requests for
missing files (using the ErrorDocument directive), you may want to
make sure that your script generates the proper status code.
The optional replace parameter indicates whether the header
should replace a previous similar header, or add a second header of
the same type. By default it will replace, but if you pass in FALSE as
the second argument you can force multiple headers of the same type.
Forces the HTTP response code to the specified value. Note that this
parameter only has an effect if the string is not empty.
===
Return Values:
No value is returned.
Example:
<?php
header('Location: http://www.example.com/'); //redirect to www.example.com
?>
Note: The function must be called before any output is sent, i.e. before any HTML tags or before any echo calls.
in your case:
<?php
header('Content-Length: ' . sprintf('%u', filesize($filepath)));
?>
the replace parameter which is by default - true, will cause your program to overwrite all previously set 'Content-Length' headers..
Hope all the explanation was worth it... :)