When serving files with X-Sendfile
, is it possible to have the number of bytes actually sent to the client? Something like this in pseudo code:
header("X-Sendfile: /files/2GB_file.zip");
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="2GB_file.zip"');
// don't return here, instead wait until the file is served (or the Range request is served)
$size = get_number_of_bytes_served();
log_to_database($size);
Obviously, we can find this is the Apache log:
example.com:80 1.2.3.4 - - [19/Jun/2018:08:46:30 +0200] "GET /test.txt HTTP/1.0" 200 108893 "-"
(here it's 108893
), but it would be useful to have it directly in PHP, immediately once the request is finished. This would avoid having a crontab script that parses the Apache logs periodically, etc. in order to get this information.
Note: I'm using X-Sendfile
for performance reason (see Fastest Way to Serve a File Using PHP and Downloading files with download.php).