【发布时间】:2009-10-29 11:30:35
【问题描述】:
有没有办法记录(记录)通过浏览器从网络服务器下载文件所花费的时间?文件写在硬盘上,环境是LAMP。
谢谢。
【问题讨论】:
有没有办法记录(记录)通过浏览器从网络服务器下载文件所花费的时间?文件写在硬盘上,环境是LAMP。
谢谢。
【问题讨论】:
当然有。您将不得不使用 mod_rewrite 让 Apache 知道该文件将由 PHP 提供(如果是这种情况,请询问更多说明),或者只使用 PHP 脚本来获取文件,如下所示:
http://youserver.com/download.php?filename=mypicture.jpeg
然后你可以像这样拥有download.php:
<?php
// gets the starting time
$time_start = microtime(true);
// WATCHOUT! THIS IS NOT SECURE! EXAMPLE ONLY.
#$filename = $_GET['filename'];
// gets the intro.mp3 file and outputs it to the user
$filename = "intro.mp3";
header('Content-type: audio/mpeg');
header('Content-Length: '.filesize($filename));
header('Content-Disposition: attachment; filename="intro.mp3"');
readfile($filename);
// gets the end time and duration
$time_end = microtime(true);
// write time to hdd, database, whatever
// ...
error_log("Processing time: ". sprintf("%.4f", ($time_end-$time_start))." seconds");
?>
请记住,$filename = $_GET['filename'] 仅作为示例,应正确转义,以免人们侵入您的服务器。
编辑:
更改以检查它是否真的有效 - 马克让我质疑它! ;) 需要进行微调(尤其是在微时间上),但是的,它确实有效!
【讨论】:
一种方法是通过脚本代理下载(可以通过 mod_rewrite 轻松实现)。这意味着您需要完成所有工作,例如将扩展映射到其相应的 mime 类型(对于 Content-Type 标头)。尝试从浏览器下载文件,并查看它发送的标头;你需要模仿那些。您可以使用例如。 web-sniffer.net 来检查标题。
【讨论】:
我认为 PHP 无法访问此类信息。我不确定 Apache 是否会告诉你,或者下面的方法是否也会计时,直到文件被转储到传输缓冲区,但我建议你试试这个:
定义一个自定义日志,其中包含“%D”指令,这将为您提供“服务请求所用的时间,以微秒为单位。”
【讨论】: