Gossamer Forum
Home : Products : Gossamer Links : Discussions :

HOWTO: Using File Columns

Quote Reply
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
Subject Author Views Date
Thread HOWTO: Using File Columns brewt 20498 Jul 27, 2006, 8:49 PM
Thread Re: [brewt] Using File Columns
Payooo 19429 Jul 28, 2006, 4:19 AM
Thread Re: [Payooo] Using File Columns
brewt 19413 Jul 28, 2006, 12:29 PM
Post Re: [brewt] Using File Columns
Payooo 19369 Jul 29, 2006, 8:00 AM
Post Re: [brewt] Using File Columns
Payooo 19301 Aug 21, 2006, 3:48 AM
Thread Re: [brewt] HOWTO: Using File Columns
MJ_ 19457 Sep 25, 2006, 6:40 AM
Thread Re: [Oyo] HOWTO: Using File Columns
brewt 19304 Sep 25, 2006, 12:39 PM
Thread Re: [brewt] HOWTO: Using File Columns
MJ_ 19375 Sep 25, 2006, 12:45 PM
Thread Re: [Oyo] HOWTO: Using File Columns
brewt 19523 Sep 25, 2006, 1:04 PM
Thread Re: [brewt] HOWTO: Using File Columns
MJ_ 19240 Sep 25, 2006, 1:11 PM
Thread Re: [Oyo] HOWTO: Using File Columns
brewt 19372 Sep 25, 2006, 1:39 PM
Post Re: [brewt] HOWTO: Using File Columns
MJ_ 19282 Sep 25, 2006, 2:02 PM
Thread Re: [brewt] HOWTO: Using File Columns
MJ_ 19242 Sep 25, 2006, 2:39 PM
Thread Re: [Oyo] HOWTO: Using File Columns
brewt 19219 Sep 28, 2006, 12:54 PM
Thread Re: [brewt] HOWTO: Using File Columns
colintho 18906 Jun 10, 2007, 9:36 AM
Thread Re: [colintho] HOWTO: Using File Columns
Andy 18975 Jun 11, 2007, 1:20 AM
Thread Re: [Andy] HOWTO: Using File Columns
colintho 18945 Jun 11, 2007, 1:47 AM
Post Re: [colintho] HOWTO: Using File Columns
colintho 18920 Jun 11, 2007, 11:11 AM
Thread Re: [Andy] HOWTO: Using File Columns
Janio 11418 Dec 30, 2010, 12:09 PM
Post Re: [Janio] HOWTO: Using File Columns
Janio 11316 Dec 31, 2010, 8:33 AM
Post Re: [brewt] HOWTO: Using File Columns
MJ_ 19222 Sep 25, 2006, 1:32 PM
Post Re: [brewt] HOWTO: Using File Columns
MJ_ 19222 Sep 25, 2006, 3:08 PM
Thread Re: [brewt] HOWTO: Using File Columns
MJ_ 19380 Sep 26, 2006, 2:08 AM
Thread Re: [Oyo] HOWTO: Using File Columns
brewt 19190 Sep 26, 2006, 11:59 AM
Post Re: [brewt] HOWTO: Using File Columns
MJ_ 19177 Sep 26, 2006, 12:24 PM
Thread Removed
r78RE 18611 Dec 2, 2007, 5:08 AM
Thread Re: [turischt] HOWTO: Using File Columns
Andy 18798 Dec 2, 2007, 5:35 AM
Thread Removed
r78RE 11824 Dec 2, 2007, 5:50 AM
Thread Removed
r78RE 11779 Dec 4, 2007, 10:40 AM
Thread Re: [turischt] HOWTO: Using File Columns
Andy 11899 Dec 5, 2007, 1:59 AM
Thread Removed
r78RE 11750 Dec 6, 2007, 8:21 AM
Thread Re: [turischt] HOWTO: Using File Columns
colintho 11864 Feb 4, 2008, 9:43 AM
Thread Re: [colintho] HOWTO: Using File Columns
Andy 11716 Feb 4, 2008, 10:14 AM
Thread Re: [Andy] HOWTO: Using File Columns
colintho 11678 Feb 4, 2008, 1:03 PM
Thread Re: [colintho] HOWTO: Using File Columns
Andy 11764 Feb 5, 2008, 1:08 AM
Thread Re: [Andy] HOWTO: Using File Columns
colintho 11642 Feb 5, 2008, 1:17 AM
Thread Re: [colintho] HOWTO: Using File Columns
Andy 11830 Feb 5, 2008, 1:21 AM
Post Re: [Andy] HOWTO: Using File Columns
colintho 11664 Feb 5, 2008, 1:33 AM
Thread Re: [colintho] HOWTO: Using File Columns
Janio 11319 Dec 30, 2010, 12:16 PM
Thread Re: [Janio] HOWTO: Using File Columns
Andy 11385 Dec 30, 2010, 11:59 PM
Thread Re: [Andy] HOWTO: Using File Columns
Janio 11343 Dec 31, 2010, 8:16 AM
Post Re: [Janio] HOWTO: Using File Columns
Andy 11333 Dec 31, 2010, 8:29 AM
Post Re: [Andy] HOWTO: Using File Columns
Janio 11276 Dec 31, 2010, 8:39 AM