what is the best method to track direct file downloads? I found some solution, for example this one:
http://www.gayadesign.com/diy/download-counter-in-php-using-htacces
Your apache logs should contain a lot of info, but I think what you're asking for is more control over what gets logged and when. So what you want to do is have two pages: one with the link to the file, and the other that tracks the file, like so:
file_page.php
Download File!
download.php
// Code to track the file using PHP, whether that means storing data in a database, saving to a log, or emailing you. I'd use a DB, like so:
// Prep the vars
$file_id = $_GET['file_id']; // You should sanitize this first.
$file_path = '/files/'.$file_id.'.pdf';
// Save data to database
mysql_query('INSERT INTO download_log
SET file_id = '.$file_id.',
date_downloaded = '.date('Y-m-d H:i:s').',
user_id = '.$_SESSION['user_id']);
// Now find the file and download it
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename='.$file_id.'.pdf); // or whatever the file name is
readfile($file_path);
Something like that, anyway.
The page will be blank when it is done, but all browsers should begin downloading the file when the page is loaded.
So what I'm doing here is I'm saving the file ID, the current datetime, and the user ID of the person downloading it (from a $_SESSION variable). You probably want to store a lot more information, such as the user's IP address, the HTTP_REFERRER or other $_SERVER information, so you can track where the user came from and when and what they downloaded.
Good luck.