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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Statistics for uploaded files?


Go to End


2 Posts   1602 Views

Avatar
atomicguava

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?

Avatar
bummzack

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: http://php.net/manual/en/function.fread.php
3) Create a custom rule for your controller (http://doc.silverstripe.com/doku.php?id=director#custom_rewrite_rules). 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.

Correction:
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.