Counting file downloads
From FileRun Documentation
Applies to FileRun version (141109A)
To keep track of the files' download count, please follow these steps:
- Go to "Control Panel » System configuration » Metadata » Fieldsets » Create new"
- Set the "Fieldset name" (Something like "Info", it doesn't really matter)
- Tick the "Generic fieldset" checkbox. (So that all files show this fieldset)
- Click "Save" to add the fieldset.
- Click "Manage Fields > Create new"
- Type "Downloads" for the "Field name" and click "Save"
- By holding the mouse cursor over the newly created field you can find out its ID, from the link's URL (......&fid=X)
- Create the text file "/path-to-filerun/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.
