Counting file downloads

Applies to Web File Share version (260213)

To keep track of the files' download count, please follow these steps:

  • Login as superuser and open the Control Panel
  • Select "System configuration  » Metadata  » Fieldsets" and click "Add"
  • Set the "Field set name" (Something like "Info", it doesn't really matter)
  • Enable the "Generic field set" option (So that all files show this field set)
  • Click "Add Field Set"
  • Activate the "Field sets" tab and double-click in the list the newly created field set
  • Inside the "Fields" section, click "Add".
  • Type "Downloads" for the "Field name" and click "Save".
  • You need the ID of the recently added field. You can get that directly from the MySQL database, by browsing the table "df_modules_metadata_fields"
  • Create the text file "/path-to-WebFileShare/customizables/events/download.php" and paste the following code inside:

<?php

 

$fieldId = XXXX; //set here the ID of the metadata field that will be holding the downloads count

 

/*-----------------------------------------------------------------*/

 

global $metadata, $db;

$data = unserialize($data['data']);

$metaFileInfo = $metadata->files->getByPath("*", $data['full_path']);//get file metadata record

 

if (!$metaFileInfo['id']) {

                $id = $metadata->files->addFile($data['full_path']);//add file metadata record if not found

                if ($id) {

                                $metaFileInfo['path'] = $data['full_path'];

                                $metaFileInfo['id'] = $id;

                }

}

 

$rs = $metadata->get($metaFileInfo['id'], array($fieldId));//get current download count

 

$downloads = $rs[0]['val'];

if (!$downloads) {

                $downloads = 0;

}

$metadata->set($metaFileInfo['id'], $fieldId, $downloads+1);//increment the download count

  • Replace the "XXXX" with the ID of your "Downloads" metadata field on the first line of the script file.

The "download.php" script will execute each time a user downloads a file, and it will increment the value it finds in the file's "Downloads" metadata field. You can reset the count at any time, by editing the value from the Metadata menu.

Being a metadata field, you can also display it as a column in the file list view, to have a quick view over how many time the files were downloaded.

 

Attached Files
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Name
Email
Security Code Security Code
Related Articles RSS Feed
Automatic Login
Viewed 2330 times since Wed, Nov 22, 2017
The API
Viewed 4308 times since Wed, Nov 22, 2017
Hiding options from the "Open with..." menu
Viewed 2278 times since Thu, Mar 6, 2014
Custom even scripts
Viewed 2558 times since Wed, Nov 22, 2017
Custom "Open with" actions
Viewed 2137 times since Wed, Nov 22, 2017
Running custom scripts when users perform various actions
Viewed 3343 times since Thu, Mar 6, 2014
Adding links to the menu
Viewed 2512 times since Thu, Mar 6, 2014
Translating Web File Share
Viewed 20070 times since Wed, Mar 5, 2014
Custom CSS
Viewed 1952 times since Wed, Nov 22, 2017
File reference
Viewed 2461 times since Tue, Jun 10, 2014
MENU