9

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
Eric Aya
  • 69,000
  • 34
  • 174
  • 243
David Koelle
  • 20,370
  • 23
  • 91
  • 129

4 Answers4

12

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.

Sean Bright
  • 114,945
  • 17
  • 134
  • 143
  • does this tell you if the download was completed or cancelled? – Steven A. Lowe Oct 01 '08 at 16:03
  • -1. Please see the next answer.. its simpler than parsing a log – bobobobo Feb 11 '11 at 21:29
  • 4
    Simpler to write a script that increments a counter and then forwards the request to another file? Alrighty. – Sean Bright Feb 11 '11 at 22:09
  • I just spend some time trying to find out _if_ I have access to the logs, and _where_ I possibly could find them. A hint regarding default locations would be great. Otherwise one could easily spend as much time searching for the logs as it takes to write a script that increments a counter and... especially if it turns out that you don't have the logs like in my case :) – bluenote10 Jun 13 '15 at 19:23
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.

Chris
  • 2,816
  • 23
  • 24
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.

biegleux
  • 13,114
  • 11
  • 44
  • 52
0

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

Max Cantor
  • 8,079
  • 6
  • 43
  • 59