Jasper
Jasper

Reputation: 5238

How to count requests to file

Like, I have file on ftp http://site.com/download/file.zip

I download it by directly request from browser's address tab.

How can I count the number of requests of this file?

Or how do I remove the ability of such requests, so they should work only by php?

Upvotes: 0

Views: 2301

Answers (4)

Jon
Jon

Reputation: 437854

If anyone can get at the file by typing in a URL, you can't really count the accesses in any way other than reading the web server access log.

What you can do is:

  1. Make the file itself inaccessible through any URL
  2. Write a PHP script that "serves files" according to a query parameter passed to it
  3. Keep a count from inside this script

Generally, the script in step 2 will look somewhat like this:

// Increase your "download count" by one

// $mimeType is the MIME type of the file you are serving
//     e.g. "application/octet-stream"
// $filename is the name that the browser will offer as a default
//     in the "save file" dialog
// $filepath is the real path of the file on your web server

header('Content-Type: '.$mimeType);
header('Content-Disposition: attachment; filename="'.$filename. '";' );
header('Content-Length: '.filesize($filepath));

readfile($filepath);
die;

Upvotes: 2

denislexic
denislexic

Reputation: 11402

If you are not a web guru. You can do what @Ale said by putting it in a file and then creating the file. In that file, put Google analytics and just track it from there. You'll have everything, even where they are, how many different people...so on.

Hope this helps.

Upvotes: 0

Florian
Florian

Reputation: 3191

You can remove the ability to direct access the file with a .htaccess file :

<FilesMatch ~ "^file\.zip$">
    Deny from all
</FilesMatch>

Upvotes: 0

Ale
Ale

Reputation: 443

You can create a download.php file that processes the download. I mean:

http://site.com/download.php?dl=file

And in such file you do whatever you want (log the timestamp, increase the number of downloads...). Then redirect to download the file.

Upvotes: 1

Related Questions