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

NOTE: This article is 3 years or older so its information may no longer be relevant. Read on at your own discretion! Comments for this article have automatically been locked, refer to the FAQ for more details.
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:
[download=file.ext]


This produces a link like this:
downloadctr1.png



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:
downloadctr2.png


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:
downloadctr3.png


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


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] (448 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.

-i

Skip down to comments...
Hope you found this post useful...

...so please read on! I love writing articles that provide beneficial information, tips and examples to my readers. All information on my blog is provided free of charge and I encourage you to share it as you wish. There is a small favour I ask in return however - engage in comments below, provide feedback, and if you see mistakes let me know.

If you want to show additional support and help me pay for web hosting and domain name registration, donations, no matter how small, are always welcome!

Use of any information contained in this blog post/article is subject to this disclaimer.
 
comments powered by Disqus
Other posts you may like...