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 1285 times since Wed, Nov 22, 2017
External Login Form
Viewed 1568 times since Wed, Nov 22, 2017
Custom file actions
Viewed 1600 times since Wed, Mar 5, 2014
Custom even scripts
Viewed 1503 times since Wed, Nov 22, 2017
Custom "Open with" actions
Viewed 1291 times since Wed, Nov 22, 2017
The API
Viewed 3391 times since Wed, Nov 22, 2017
Translating Web File Share
Viewed 19223 times since Wed, Mar 5, 2014
Forcing certain file types to download
Viewed 1761 times since Thu, Mar 6, 2014
File reference
Viewed 1623 times since Tue, Jun 10, 2014
Custom CSS
Viewed 1096 times since Wed, Nov 22, 2017
MENU