Gossamer Forum
Home : Products : Gossamer Links : Discussions :

HOWTO: Using File Columns

(Page 1 of 2)
> >
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
Quote Reply
Re: [brewt] Using File Columns In reply to
Gossamer Links 3.2.0? Smile
Where can we find it?
Thanks!

Quote Reply
Re: [Payooo] Using File Columns In reply to
It's not out yet. It will probably be released some time next week.

Adrian
Quote Reply
Re: [brewt] Using File Columns In reply to
Smile

Thanks!

You can delete my posts if you wish ...
Quote Reply
Re: [brewt] Using File Columns In reply to
In Reply To:
It's not out yet. It will probably be released some time next week.
I am still waiting for the next week Cool
Quote Reply
Re: [brewt] HOWTO: Using File Columns In reply to
Hello,

I have a problem with the global.

1) If the column FileColumnName is empty, the global gives an error.

A fatal error has occured:
Can't call method "File_URL" on an undefined value at (eval 55) line 3.
Please enable debugging in setup for more details.
Stack Trace
======================================
Links (29458): Links::environment called at /links/admin/Links.pm line 750 with no arguments.
Links (29458): Links::fatal called at (eval 55) line 3 with arguments
(Can't call method "File_URL" on an undefined value at (eval 55) line 3.
).
Links (29458): Links::__ANON__ called at GT::Template::_call_func line 791 with arguments
(Image1, 6603).
Links (29458): GT::Template::_call_func called at /links/admin/templates/luna/compiled/modify_success.html.compiled line 499 with arguments
(GT::Template=HASH(0x87fce60), view_img1, 1, 0, Image1, 6603).
Links (29458): GT::Template::parsed_template called at /links/admin/GT/Template.pm line 565 with arguments
(GT::Template=HASH(0x87fce60)).
Links (29458): GT::Template::_parse called at /links/admin/GT/Template.pm line 158 with arguments
(GT::Template=HASH(0x87fce60), modify_success.html, HASH(0x871a8dc)).
Links (29458): GT::Template::parse called at /links/admin/Links.pm line 452 with arguments
(GT::Template, modify_success.html, ARRAY(0x87fcb48), HASH(0x871a8dc)).
Links (29458): Links::user_page called at /links/admin/Links/SiteHTML.pm line 306 with arguments
(modify_success.html, HASH(0x88aeb2c), HASH(0x871a8dc)).
Links (29458): Links::SiteHTML::__ANON__ called at /links/admin/GT/Plugins.pm line 133 with arguments
(HASH(0x88aeb2c), [undef]).
Links (29458): GT::Plugins::dispatch called at /links/admin/Links/SiteHTML.pm line 27 with arguments
(GT::Plugins=HASH(0x83ab040), site_html_modify_success, CODE(0x8672814), HASH(0x88aeb2c), [undef]).
Links (29458): Links::SiteHTML::display called at /links/admin/Links/User/Modify.pm line 166 with arguments
(modify_success, HASH(0x88aeb2c)).
Links (29458): Links::User::Modify::_modify called at /links/admin/Links/User/Modify.pm line 32 with no arguments.
Links (29458): Links::User::Modify::handle called at /links/admin/GT/Plugins.pm line 133 with no arguments.
Links (29458): GT::Plugins::dispatch called at modify.cgi line 26 with arguments
(GT::Plugins=HASH(0x83ab040), handle_modify, CODE(0x82ea348)).
----------------------------------------


2) The condition does not give any result.

<%if FileColumnName%>
<img src="<%filecol_url('FileColumnName', $ID)%>" alt="" />
<%endif%>

The use of DUMP indicates that the column FileColumnName is empty in modify_success.html.

You have an idea ?

I use Gossamer Links 3.2.0

Thank you for your assistance.

Oyo

Last edited by:

Oyo: Sep 25, 2006, 6:49 AM
Quote Reply
Re: [Oyo] HOWTO: Using File Columns In reply to
What's the actual global you are using and the column name and the template code?

Adrian
Quote Reply
Re: [brewt] HOWTO: Using File Columns In reply to
I use:

<%view_img1%>

sub {
my ($col, $id) = @_;
return $DB->table('Links')->file_info($col, $id)->File_URL;
}

column name : Image1

in modify_success.html

<img src="<%view_img1('Image1', $ID)%>" alt="" />


You have an idea ?

Thank you

Last edited by:

Oyo: Sep 25, 2006, 12:55 PM
Quote Reply
Re: [Oyo] HOWTO: Using File Columns In reply to
You have to have <%if Image1%><img src="..." /><%endif%> around it in the case that Image1 is not set (ie. an image wasn't uploaded).

Adrian
Quote Reply
Re: [brewt] HOWTO: Using File Columns In reply to
I also tested this but the condition does not give a result.

The use of <%DUMP%> (in modify_success.html) indicates that the column Image1 is empty.

An idea ?
Quote Reply
Re: [brewt] HOWTO: Using File Columns In reply to
In add_success.html <%Image1%> has the value :

'C:\My documents\My images\my_image.gif';

That is normal ?

Thank you for your assistance.

Last edited by:

Oyo: Sep 25, 2006, 1:38 PM
Quote Reply
Re: [Oyo] HOWTO: Using File Columns In reply to
If you have user_direct_mod turned off then Image1 will not be set since the file isn't viewable until it's been validated.

Adrian
Quote Reply
Re: [brewt] HOWTO: Using File Columns In reply to
It is possible to obtain a value for <%Image1%> in modify_success.html ?

Currently, even if an image is present in the column (for the link), the tag does not give a value.

Thank you

Oyo

Last edited by:

Oyo: Sep 25, 2006, 2:03 PM
Quote Reply
Re: [brewt] HOWTO: Using File Columns In reply to
I have another problem.

I have the option on Yes for user_direct_mod, but I always have the message :

You are not authorized to modify this link.

user_required is on Yes...


Thank you for your assistance.

Oyo

Last edited by:

Oyo: Sep 25, 2006, 2:42 PM
Quote Reply
Re: [brewt] HOWTO: Using File Columns In reply to
   
I believe that the problem is known, but not solution.

If you have an idea...

http://www.gossamer-threads.com/...i?post=251141#251141

Thank you

Oyo
Quote Reply
Re: [brewt] HOWTO: Using File Columns In reply to
Hello,

I have can be found my problem.

In modify_success.html (with filecolumn.html in include_form.html) :

a) There is an image, the user modifies the link but not the image.
Image1 eq ' '
Image1_del eq '0'

b) There is an image, the user removes the image.
Image1 eq ' '
Image1_del eq '1'

c) There is an image, the user replaces the image.
Image1 eq '/path/to/location/Links/admin/tmp/my_image.gif'
Image1_del eq '0'
Image1_filename eq 'my_image.gif'

d) There is no image, the user adds an image.
Image1 eq '/path/to/location/Links/admin/tmp/my_image.gif'
Image1_filename eq 'my_image.gif'



*** C and D cause the error :

Can't call method "File_URL" on an undefined value at (eval 58) line 3.

for :

<%if Image1%>
<img src="<%view_img1('Image1', $ID)%>" alt="" />
<%endif%>

<%view_img1%>

sub {
my ($col, $id) = @_;
return $DB->table('Links')->file_info($col, $id)->File_URL;
}

--------------------
My question.

Why (/admin/tmp/) : /path/to/location/Links/admin/tmp/

It would be correct to use :
File Save Location : /path/to/location/to/save/files


Therefore, how to modify the program so that the file is downloaded in 'File Save Location' ?


Thank you for your answer.

Oyo

Last edited by:

Oyo: Sep 26, 2006, 2:10 AM
Quote Reply
Re: [Oyo] HOWTO: Using File Columns In reply to
You cannot use the normal file column code with links in the Changes table (ie. links awaiting change validation) because the data isn't associated with the Links table yet. If you still want to make the file data available, you have to write your own code to use it. The file is stored in the tmp location until it is validated.

Adrian
Quote Reply
Re: [brewt] HOWTO: Using File Columns In reply to
Thank you.

I always have a problem with user_direct_mod.
http://www.gossamer-threads.com/...i?post=294118#294118


It is Bug?

Thank you for your answer.

Oyo
Quote Reply
Re: [Oyo] HOWTO: Using File Columns In reply to
In Reply To:
I have the option on Yes for user_direct_mod, but I always have the message :

You are not authorized to modify this link.
That should only happen if you the user attempting to modify the link isn't the owner of the link.

Adrian
Quote Reply
Re: [brewt] HOWTO: Using File Columns In reply to
Hi

very useful post, just what I was looking for.

You mention this earlier in the post: "so I have attached to the post instead (filecolumn.html)"

I couldn't see the attachment.
regards

Colin
Colin Thompson
Quote Reply
Re: [colintho] HOWTO: Using File Columns In reply to
Hi,

The attachment is there :) Here's a direct link to it.

http://www.gossamer-threads.com/...ment;postatt_id=2060

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] HOWTO: Using File Columns In reply to
Thanks Andy. Smile
Colin Thompson
Quote Reply
Re: [colintho] HOWTO: Using File Columns In reply to
Hi

I'm trying to use the image filecol_url code from above to produce thumbnail image links that display the larger image in an inline frame. This works OK if all 5 images have been uploaded, but produces an error, something like the one mentioned above if less thank 5 images have been uploaded. (A fatal error has occured: Can't call method "File_URL" on an undefined value at.....)


I think its something to do with the need to use the <%if > <%endif%> code.

This what I have at present...please can you help?

<p><a target="house_for_sale" href="<%filecol_url('Image', $ID)%>"><%if Smallimage%><img src="<%filecol_url('Smallimage', $ID)%>" alt="House for Sale" hspace="0" /><%endif%></a></p>


Thanks
Colin Thompson
Quote Reply
Removed In reply to
Removed

Last edited by:

turischt: Dec 2, 2007, 5:09 AM
Quote Reply
Re: [turischt] HOWTO: Using File Columns In reply to
Hi,

The first test, is to goto Database > Links > Modify, and modify an existing link. Try uploading an image to your new field. Then goto Database > Links > Search, and enter that ID again. You should see a "view" and "download" link next to your FILE field - which will show you the uploaded file (if its setup right Smile

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
> >