Best way to count file downloads on a website

ε祈祈猫儿з 提交于 2019-11-30 05:59:26

问题


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. What can I do to find out how many people have accessed this file?
  3. I could use Tomcat calls if necessary

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


回答1:


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

grep foo.zip /path/to/access.log | 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:


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:


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.




回答4:


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



来源:https://stackoverflow.com/questions/158124/best-way-to-count-file-downloads-on-a-website

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!