Home : Products : Gossamer Links : Discussions :

Products: Gossamer Links: Discussions: HOWTO: Using File Columns: Edit Log

Here is the list of edits for this post
HOWTO: Using File Columns
Just to expand a bit on Andy's File Column example thread, here are some instructions on using file columns. The template sample is designed for Gossamer Links 3.x, but the instructions also apply to Links SQL 2.x as well.

Note that there are some bugs in filename handling (ie. filenames with spaces, etc) with Links SQL and Gossamer Links 3.1.0 or lower. However, Gossamer Links 3.1.0 has updates to fix most of these issues. Gossamer Links 3.2.0 and higher have these issues resolved.

What are file columns?
File columns are extra columns added to your tables that allow you to associate a file with records of that table. For example, it can give your users the ability to attach images, documents, etc to a link. Another possibility would be to add a file column to your Category table and assign an image to each category.

Adding a file column
To add a file column, you need to use the database editor: Database => Editor (menu on left) => select table (eg. Links) => Properties => Go
Then at the bottom of the page, click Add Column

Here are the settings that need to be changed:
Quote:
Column Name: FileColumnName
Column Type: VARCHAR (or CHAR)
Column Size: 255
Form Type: FILE
File Save Location: /path/to/location/to/save/files (this directory must already exist)
File Save URL: http://some.url/to/files (optional, see "Link to the file" below)
File Save Method: HASHED
File Maximum Size: size in bytes (optional)
Form Regex: (optional, see "Restricting the filename" below)
Changing the Add/Modify form
You will need to modify the include_form.html template before your users will have the ability to upload files. The template causes the forum to scroll, so I have attached to the post instead (filecolumn.html). To use it, you need to replace FileColumnName with the name of the file column. The template also allows users to delete or replace the file when modifying the link.

Linking to the file
There are two ways of linking to the file:
  1. Using jump.cgi:
    Code:
    <%if FileColumnName%>
    <img src="<%config.db_cgi_url%>/jump.cgi?ID=<%ID%>;view=FileColumnName" alt="" />
    <%endif%>
    If you aren't displaying the file on the page and want the browser to open a "Save As" dialog instead, then change "view" to "download":
    Code:
    <%if FileColumnName%>
    <a href="<%config.db_cgi_url%>/jump.cgi?ID=<%ID%>;download=FileColumnName">Download</a>
    <%endif%>
  2. Using a global to get the URL of the file. Note that this requires that you set the "File Save Location" to a location that is accessible from the Internet and have a valid File Save URL.

    First add the template code to display the file (in this example, I have named the global "filecol_url"):
    Code:
    <%if FileColumnName%>
    <img src="<%filecol_url('FileColumnName', $ID)%>" alt="" />
    <%endif%>
    Then add the global (giving it the name you used above). If you are using Links SQL 2.x, or Gossamer Links 3.1.0 or lower, then you will have to use this slightly longer global:
    Code:
    sub {
    my ($col, $id) = @_;
    my $finfo = $DB->table('Links')->file_info($col, $id);
    return unless $finfo;
    my $path = $finfo->File_RelativePath;
    require GT::File::Tools;
    my $file = $IN->escape(GT::File::Tools::basename($path));
    $path = GT::File::Tools::dirname($path);
    return "$path/$file";
    }
    Gossamer Links 3.2.0 or higher users can use the following global:
    Code:
    sub {
    my ($col, $id) = @_;
    my $finfo = $DB->table('Links')->file_info($col, $id);
    return $finfo->File_URL if $finfo;
    }
The benefit of using method 1 is that the filenames of the file are kept the same on download and you have the ability to force the file to be saved rather than viewed. For method 2, the filenames are the modified to contain the file ID and are escaped, but are accessed directly rather than through jump.cgi.

Restricting the filename
By adding a regular expression on the column, you can restrict the filenames that can be attached. Note that it doesn't guarantee that the file is actually the type that the filename says it is. Here are some examples (the "(?i)" at the beginning makes it case insensitive):
Code:
# Allow no file to be attached or an image of type jpeg, gif, or png
(?i)^(?:|[\w-\. ]+\.(?:jpg|jpeg|jpe|gif|png))$
# A pdf must be attached
(?i)^[\w-\. ]+\.pdf$

Adrian

Last edited by:

brewt: Nov 16, 2007, 4:17 PM

Edit Log: