Etomite Community Forums: [Snippet] DownloadCounter - Etomite Community Forums

Jump to content

Read Me

This forum is posted in automatically when a snippet is added to the library, or updated. Please use the snippet topic to discuss that snippet.

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Snippet] DownloadCounter

#1 User is offline   Cris D. 

  • Loves Etomite Forums!
  • PipPipPipPip
  • Group: Member
  • Posts: 1,063
  • Joined: 10-August 06
  • Location:Brisbane, Queensland, Australia

Posted 27 October 2007 - 10:12 PM

File Name: DownloadCounter
File Submitter: Cris D.
File Submitted: 28 Oct 2007
File Updated: 1 Nov 2007
File Category: Miscellaneous

READ ME for Snippet: DownloadCounter
Version: Ver 1.3 (beta).
Author: Cris D.
Date: 2007/10/28
See in action here.
Credits:
Etomised and secured from the original script found here.
And php's filesize_readable function from here.

DownloadCounter generates:

filename - download link - custom message number of downloads - filesize.

example:
image.jpg Download it Now This has been downloaded 23 times (36kB)

It also can be (optionally) used to stop unauthorized downloads by checking permissions of a user.

INSTALLATION INSTRUCTIONS:

For ease of installation, these instructions will describe how to set up this snippet with its default settings. However, most of these steps can be customised if the changes are reflected in the snippet if you know what you are doing.

1) Create a folder called downloads in the same folder as your index.php file (etomite root).

2) Inside that create a folder called counters.

You should now have two folders:

downloads
downloads/counters

CHMOD the counters folder to 755 if it is not already (this can be tightened up later if necessary but it must be readable and writable).

3) Using ftp or etomite's File Manager, place the files you want to be downloaded in the 'downloads' folder (take note of the name and extention).

4) Copy and paste the snippet into your snippet library, call it DownloadCounter.

5) Place the snippet in the page where you want the download link to show.

The snippet generates:
Scenario 1) ** IF BROWSER HAS ACCESS TO THE DOWNLOAD LINK**

"filename.ext: <a href>Download</a> This file has been downloaded [nn] times."

Scenario 2) ** IF SECURITY HAS BEEN SET AND USER IS NOT LOGGED IN**

"filename.ext You are not authorised to download: download2.txt
<a href>Contact the webmaster</a>."

Scenario 3) ** IF FILE DOES NOT EXIST OR THERE IS A PROBLEM LOCATING IT **

"download2.txt This link to downloads/filename.ext does not point to a valid file."


6) Write the snippet call to a) Point the snippet to the particular file B) set the level of security for just that file. c) set the download_dir if NOT in the downloads folder

6 a) Pointing the snippet to the file is done by adding
[!DownloadCounter?fileName=name_of_file.extention!]

6 B) Setting the security for the link (optional) is done by adding
[!DownloadCounter?fileName=name_of_file.extention&secure=true!]
6 c) Set the download_dir if the file is stored outside the downloads folder by adding
[!DownloadCounter?download_dir=assets/images&filename=cloud.jpg!]


So to set the download of a file called cat.jpg and make it secure, you would place
[!DownloadCounter?fileName=cat.jpg&secure=true!] in the page where you want the Senario 1 or 2 to appear.

#### OPTIONAL SNIPPET CONFIGURATION #########

  • The folder where the file is stored it can also be set in the snippet call to point to a different dir
    eg [!DownloadCounter?download_dir=assets/images&fileName=cloud.jpg!]
    $download_dir=isset($download_dir)? $download_dir : 'downloads'; //this is the default directory to look in
  • $counter_dir = 'downloads/counters'; // the folder where your counter files are stored (CHMOD to 755)
  • Set whether the default is secure of not. (This can be overridden in the snippet call).
    $secure=isset($secure)? $secure:false;
  • $deny=" You are not authorised to download: ";//message to set for output
  • $redirectPage=58;//set to the id of the page where you can be contacted if you want to prompt users to 1) set up an account 2) get a membership 3) check thier log-in details etc

  • Set a default file to download if not called in the snippet call (suggested of you are a lazy web housekeeper): It should stop the error message for file not found from showing. However, you will need to actually put that file there!
    $fileName= isset($fileName) ? $fileName:'download1.txt';
  • This will read

    Quote

    $DownloadMessage [nn] $times.

    $DownloadMessage=" This has been downloaded ";
    $times=" times";

  • $downloadNow="Download";//this can also point to an <img src=\'assets/images/download_button.gif\'alt=\'download\' />

CHANGELOG:
2007/10/28
1.2 Added text counter file write function to reduce the code.
Added capability to set the download directory from the snippet call.
Improved Error reporting.
1.3 Added file size in human readable format

Click here to download this file

This post has been edited by Cris D.: 01 November 2007 - 10:11 AM


#2 User is offline   Cris D. 

  • Loves Etomite Forums!
  • PipPipPipPip
  • Group: Member
  • Posts: 1,063
  • Joined: 10-August 06
  • Location:Brisbane, Queensland, Australia

Posted 27 October 2007 - 11:13 PM

Note: I've just realized there is a bug if the snippet is called more than once on the page because it the counter is incrementing by the number of downloads on the page instead of by 1. This is only an issue if you have more than one download on a page. I intend on fixing it : but I will have to come back to it a bit later. [EDIT: DONE]

[EDIT Added the ability to set the download directory from the snippet call so you can link to files outside the downloads directory, counter files are still all stored in the same place regardless of where the download file is stored.

Added better error reporting.[/EDIT]

This post has been edited by Cris D.: 28 October 2007 - 04:17 AM


#3 User is offline   Cris D. 

  • Loves Etomite Forums!
  • PipPipPipPip
  • Group: Member
  • Posts: 1,063
  • Joined: 10-August 06
  • Location:Brisbane, Queensland, Australia

Posted 28 October 2007 - 07:10 AM

Added filesize as well: in human readable format (B, kB, Mb, Gb,Tb etc).
eg:

Quote

download1.txt : Download This has been downloaded 76 times (34 kB )

This post has been edited by Cris D.: 28 October 2007 - 07:10 AM


#4 User is offline   Opal 

  • Etomite Forum Newbie
  • Group: Member
  • Posts: 23
  • Joined: 16-January 09
  • Gender:Female

Posted 13 May 2009 - 03:59 PM

You may need to add ob_clean(); just before the header declarations in the snippet (~line 95).

I was experiencing a bug that insisted on using the header declarations from the php page itself instead of those defined in the snippet, causing odd behaviour like .htm tacked onto files and malformed files (IE and Firefox were adding the contents of the php/html page into the download file itself!)

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users