Downloader
What is it?
This script provides a very flexible, secure and easy to use download system in one single file. It can easily be embedded in existing applications as well as being used as standalone script displaying a table of avalible downloads.
Features
- easy to use and very light weight - only 150 lines of source code in one single file
- easy to embedd in other scripts
- build-in download counter
- custom Content-Type handling
- appearance can be customized with CSS
- valid xhtml1.1
Documentation
There are several possibilities to use this script. But two things are the same for all operating modes. First, if you want to use the download counter, your webserver needs write access in the directory were you want to put the stats file in. Second property is that your download folder must not be in the document root of your webserver. This might be useful for security reasons. How to configure the script for your needs is explained below. At first the different usage modes are introduced.
Standalone Usage
This is the defaut mode. You only have to define the location of the files you want to provide for download and make sure that the permissions for writing the stats file are guaranteed or logging stats is disabled. When the script is executed it provides a nice looking, valid xhtml page with a table of the files avalible for download. The appearance can simply be customized editing the CSS code. You can rename the script to what ever you want (for example index.php).
Embedded Usage
To embedd the download system in an existing application you simply have to include the script:
include("path/downloader.php");
IMPORTANT: To make the download system working this must be done at the beginning of the script you want to embedd downloader.php in. The reason is that downloader.php sends it's own header when a file is requested for download. So the include must happen BEFORE the main script sends a header. If you don't know what this implies, then simply make sure that this:
<?php include("path/downloader.php"); ?>
is the first line of your script, were path is the path to the file downloader.php.
When you want to display a table of your downloads use this function:
<?php print_download_table(); ?>
This function outputs only plain table tags without attributes, so the appearance of this table is only influenced by (your) already available CSS code.
Of course you can build your own download table. The function manage_download_stats() returns an associative array with the content of the download stats file. The array $download_filelist contains the list of avalible downloads. The structure of this array is below-mentioned.
You can also use single links to your downloads:
<?php echo "<a href=\"$SCRIPT_NAME?download=filename\" title=\"click to download\">download this file</a>"; ?>
IMPORTANT: To generate the download stats file you have to call one of the above introduced functions one time or call the script the first time in standalone mode. Once the stats file is created, entries for new files are added automaticly. To prevent that stats are deleted when you only want to refrain a download occasionally for some reasons, the entries in the stats file are never removed. This can cause that your stats file gets cluttered when you often change the files you provide for download. So you have to clean it up from time to time manually.
Configuration
To modifiy the script for your needs you have to edit the config section at the beginning of the script. The following section will explain the meaning of the containing variables. Booleans are filled with "true" or "false" in lowercase!
$standalone
Decide if you want to use script in standalone mode or embedd it.
$custom_script_name
Normaly changing the script name doesn't matter. But in some constellations (with auto generated file list only) it might be possible that the script lists it self for download. If that problem occurs it should be solved by entering here the current name of the script.
$enable_stats
Enable the download counter.
$download_stats_file
Define were you want the stats to be stored. Enter here the path and the name of the stats file. The path can be relative or absolute. Remember to set write permissions for this directory!
IMPORTANT: In embedded mode a relative path has to be from the location of the script which includes downloader.php!
$download_folder
This is the path to the folder where the files you want to provide for download are located.
IMPORTANT: This has to be an absolute path and this string has to end with a slash!
$auto_create_filelist
When this variable is set "true", the script automatically generates the $download_filelist. In this case all files in the $download_folder are avalible for download. Directories are ignored.
$download_filelist
When you don't want the download filelist to be auto created then you can define here a static one. This has the advantage that you can define names and descriptions for your downloads. And you can access the subfolders in the download folder. The names can be different from the filenames but still has to be unique.
The structure is a two dimensional associative array. The key for every entry is the name for the download (when the download filelist is auto created the name is the filename). Every entry contains an array with the filename (optionally with the relative path inside the download folder), the content type and the description.
$headline_string
This string is displayed as headline in standalone mode.
$css_code
Here you can customize the appearance in standalone mode. Take care that you only change the CSS code and not the structure of this variable.

