Igor Kromin |   Consultant. Coder. Blogger. Tinkerer. Gamer.

This is a plugin for FlatPress that adds a BBCode 'download' tag with a link to the file to download and a counter for how many times the file has been downloaded.

To use the plugin, unzip to the fp-plugins directory, enable the plugin, make sure the fl-content/attachs directory is created and writable by the web server user.

The BBCode for this plugin looks like this:

This produces a link like this:

It is also possible to specify a name to use for the link instead of the file name like this:
[download=file.ext name="My File"]

This produces a link like this:

It is also possible to display the file size by providing a 'size' attribute in the BBcode. The following are valid values for the size attribute (lower case will also work):
Bytes: [download=file.ext size=B]
Kilobytes: [download=file.ext size=K]
Megabytes: [download=file.ext size=M]
Gigabytes: [download=file.ext size=G]
Terabytes: [download=file.ext size=T]

The plugin assumes that the file to download is located in the fp-content/attachs directory. The link is generated using the blog base URL.

The counter file is saved in the same directory as the file to download with a .dlctr extension. This file is updated every time the file is downloaded.

The plugin checks if a file exists on the system before sending it to the user (after the link is clicked). If the file cannot be found a 404 error is generated like this:

If the file is not readable (this usually means the server side permissions are not correct), a 403 error is generated like this:

If a file outside of the fp-content/attachs directory is attempted to be accessed, the plugin will return a 403 error.

Note: The directory where the file is stored MUST be writable by the web server user otherwise the download count will not updated.

Download the plugin here: downloadctr-1.3.zip [3.54 Kb] (673 downloads)

If you experience any problems with the plugin, please leave a comment and I will look into it. Alternatively you can post on the forum thread for the plugin.

Edit: Version 1.1 - Updated the post and plugin with the ability to display the file size and fixed how directories are handled so the files in directories are no longer sent to the browser with a different name.

Edit: Version 1.2 - Fixed a security bug that allowed sensitive data to be downloaded from the fp-content directory. Changed the handling of the size attribute to allow lower case parameters to be passed in.

Edit: Version 1.3 - Changed the download.php file to use io_load_file() instead of manually reading in a file.


A quick disclaimer...

Although I put in a great effort into researching all the topics I cover, mistakes can happen. Use of any information from my blog posts should be at own risk and I do not hold any liability towards any information misuse or damages caused by following any of my posts.

All content and opinions expressed on this Blog are my own and do not represent the opinions of my employer (Oracle). Use of any information contained in this blog post/article is subject to this disclaimer.
Hi! You can search my blog here ⤵
NOTE: (2022) This Blog is no longer maintained and I will not be answering any emails or comments.

I am now focusing on Atari Gamer.