Jump to:

5540 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Statistics for uploaded files?

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

Page: 1
Go to End
Author Topic: 914 Views
  • atomicguava
    Avatar
    Community Member
    3 Posts

    Statistics for uploaded files? Link to this post

    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?

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Statistics for uploaded files? Link to this post

    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.

    914 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.