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
Custom CSS
Viewed 1944 times since Wed, Nov 22, 2017
Running custom scripts when users perform various actions
Viewed 3322 times since Thu, Mar 6, 2014
Custom even scripts
Viewed 2548 times since Wed, Nov 22, 2017
Adding custom functionality
Viewed 2405 times since Thu, Mar 6, 2014
Custom file actions
Viewed 2456 times since Wed, Mar 5, 2014
The API
Viewed 4301 times since Wed, Nov 22, 2017
External Login Form
Viewed 3931 times since Wed, Nov 22, 2017
Automatic Login
Viewed 2322 times since Wed, Nov 22, 2017
Adding links to the menu
Viewed 2502 times since Thu, Mar 6, 2014
Calculating MD5 Checksums
Viewed 2641 times since Thu, Mar 6, 2014
MENU