计算网站上文件下载的最佳方式

时间:2021-09-26 07:51:33

It's surprising how difficult it is to find a simple, concise answer to this question:

令人惊讶的是,找到这个问题的简单,简洁的答案是多么困难:

  1. I have a file, foo.zip, on my website
  2. 我的网站上有一个文件foo.zip

  3. What can I do to find out how many people have accessed this file?
  4. 我该怎么做才能找出有多少人访问过这个文件?

  5. I could use Tomcat calls if necessary
  6. 如有必要,我可以使用Tomcat调用

Update: If you suggest writing a script, can you point me to a decent one?

更新:如果你建议写一个脚本,你能指点我一个体面的吗?

4 个解决方案

#1


10  

Or you could parse the log file if you don't need the data in realtime.

或者,如果您不需要实时数据,则可以解析日志文件。

cat /path/to/access.log | grep foo.zip | grep 200 | wc -l

In reply to comment:

回复评论:

The log file also contains bytes downloaded, but as someone else pointed out, this may not reflect the correct count if a user cancels the download on the client side.

日志文件还包含下载的字节,但正如其他人所指出的,如果用户在客户端取消下载,则这可能无法反映正确的计数。

#2


10  

The simplest way would probably be instead of linking directly to the file, link to a script which increments a counter and then forwards to the file in question.

最简单的方法可能是直接链接到文件,链接到一个脚本,该脚本递增计数器然后转发到相关文件。

#3


4  

With the answer "The simplest way would probably be instead of linking directly to the file, link to a script which increments a counter and then forwards to the file in question."

回答“最简单的方法可能不是直接链接到文件,而是链接到一个脚本,该脚本会递增计数器,然后转发到相关文件。”

This is additional:

这是额外的:

$hit_count = @file_get_contents('count.txt');
$hit_count++;
@file_put_contents('count.txt', $hit_count);

header('Location: http://www.example.com/download/pics.zip'); // redirect to the real    file to be downloaded

Here count.txt is a simple plain text file, storing the counter info. You can save it in a database table along with downloadable_filename.ext also.

这里count.txt是一个简单的纯文本文件,存储计数器信息。您也可以将其与downloadable_filename.ext一起保存在数据库表中。

#4


0  

Use the logs--each GET request for the file is another download (unless the visitor stopped the download partway through for some reason).

使用日志 - 文件的每个GET请求都是另一个下载(除非访问者由于某种原因中途停止下载)。

#1


10  

Or you could parse the log file if you don't need the data in realtime.

或者,如果您不需要实时数据,则可以解析日志文件。

cat /path/to/access.log | grep foo.zip | grep 200 | wc -l

In reply to comment:

回复评论:

The log file also contains bytes downloaded, but as someone else pointed out, this may not reflect the correct count if a user cancels the download on the client side.

日志文件还包含下载的字节,但正如其他人所指出的,如果用户在客户端取消下载,则这可能无法反映正确的计数。

#2


10  

The simplest way would probably be instead of linking directly to the file, link to a script which increments a counter and then forwards to the file in question.

最简单的方法可能是直接链接到文件,链接到一个脚本,该脚本递增计数器然后转发到相关文件。

#3


4  

With the answer "The simplest way would probably be instead of linking directly to the file, link to a script which increments a counter and then forwards to the file in question."

回答“最简单的方法可能不是直接链接到文件,而是链接到一个脚本,该脚本会递增计数器,然后转发到相关文件。”

This is additional:

这是额外的:

$hit_count = @file_get_contents('count.txt');
$hit_count++;
@file_put_contents('count.txt', $hit_count);

header('Location: http://www.example.com/download/pics.zip'); // redirect to the real    file to be downloaded

Here count.txt is a simple plain text file, storing the counter info. You can save it in a database table along with downloadable_filename.ext also.

这里count.txt是一个简单的纯文本文件,存储计数器信息。您也可以将其与downloadable_filename.ext一起保存在数据库表中。

#4


0  

Use the logs--each GET request for the file is another download (unless the visitor stopped the download partway through for some reason).

使用日志 - 文件的每个GET请求都是另一个下载(除非访问者由于某种原因中途停止下载)。