微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

获取 X-Sendfile 提供的字节数

如何解决获取 X-Sendfile 提供的字节数

当使用 X-Sendfile 提供文件时,是否有可能将实际发送到客户端的字节数?类似这样的伪代码

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);

显然,我们可以找到这是Apache日志:

example.com:80 1.2.3.4 - - [19/Jun/2018:08:46:30 +0200] "GET /test.txt HTTP/1.0" 200 108893 "-"

(这里是 108893),但是在请求完成后立即直接在 PHP 中使用它会很有用。这将避免使用 crontab 脚本定期解析 Apache 日志等以获取此信息。

注意:我使用 X-Sendfile 是出于性能原因(请参阅 Fastest Way to Serve a File Using PHPDownloading files with download.php)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。