Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Statistics for uploaded files?

Go to End

2 Posts   1277 Views


Community Member, 3 Posts

7 August 2009 at 9:46pm

I'm just wondering if anyone has ever managed to do something like track whenever a file is downloaded, logging the event to a database (date stamp, filename etc) in order to produce a 'most popular files' list?


Community Member, 904 Posts

10 August 2009 at 6:34pm

Edited: 10/08/2009 6:45pm

I'm not aware of a out of the box solution for this. But it shouldn't be that hard to do. I would do the following:
1) Make sure all the files you want to track are copied/uploaded to a special subfolder in the assets directory (let's name it assets/filefolder. You could even protect that folder with a password, to deny direct download of files.
2) Create a controller that finds a file, writes a log entry to the statistics database and passes the file data to the user for download. The comments on the following page show how to output file-contents to the user:
3) Create a custom rule for your controller ( Something like:
'filedownload/$Action/$Filename' => 'Filedownload_Controller'
4) Use mod_rewrite to rewrite all file requests for your previously created subfolder (see 1) to your filedownload controller. Something like:

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} assets/filefolder/(.*)$
RewriteRule .* filedownload/get/%1 [L]

... and you should be up and running.
The beauty of this solution is, that once you decide to disable the logging of downloads, all you need to do is comment-out the mod_rewrite rules (and maybe remove the password protection from the file-folder if you did that). All your file-download links will still be valid!

Hope that helps.

The rewrite rule above will only work when you remove the following line from your .htacces:

RewriteCond %{REQUEST_URI} !(\.gif$)|(\.jpg$)|(\.png$)|(\.css$)|(\.js$)|(\.php$) 

.. or you could pass the filename as query string and then use $_GET['file'] in your controller to access the filename:

RewriteRule .* filedownload/get/?file=%1 [L]

On a second thought: Password protection of the files-folder isn't required, since you're redirecting all file-access anyway.