Best way to count file downloads on a website

前端 未结 4 1190
说谎
说谎 2020-12-10 15:27

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 ou
4条回答
  •  执笔经年
    2020-12-10 15:53

    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.

提交回复
热议问题