Best way to count file downloads on a website
Asked Answered
S

5

10

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
Shaer answered 1/10, 2008 at 15:12 Comment(1)
See also: dave.autonoma.ca/blog/2023/11/12/php-download-hit-counterImelda
I
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.

Imprescriptible answered 1/10, 2008 at 15:15 Comment(1)
does this tell you if the download was completed or cancelled?Lamia
W
11

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.

Watson answered 1/10, 2008 at 15:13 Comment(2)
You get download attempts, which is pretty reasonable.Cabbagehead
For a bit more information the link in this answer is pretty useful.Nickolasnickolaus
D
6

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.

Durer answered 13/9, 2012 at 11:44 Comment(1)
how can i link to a scriptAnthia
H
0

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

Hannahannah answered 1/10, 2008 at 15:13 Comment(0)
L
0

Just use cPanel metrics. Therte is a lot of information there.

Lipman answered 13/4 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.