Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

Accessing Attachments

Quote Reply
Accessing Attachments
First of all,

Thank you Alexander for your help on accessing attachments as outlined in

That was a great help.

I have a couple of other questions related to this same topic.

1. Although it's relatively easy to get attachments into the detail page (thank you Alexander) . How do we get them into a link. For example:

I'd like to be able to show an image next to my Link on a category page. How do I get it to be part of that link as is done in the admin section?
(ultimately I think the solution should allow for greater flexibility than just adding one image (i.e a list of links may include availability in several different
i.e .jpg, pdf, html, exe, .zip etc...) in which case a link should be able to like something like Title - Description - (Also available in .pdf, .exe., .jpg, .zip) where
they could be appropriately linked. My purpose is to allow for thumbnails, but I imagine others would have different uses.

My work around solution has been to manually add it as a field in my Links database, but that requires a manual insertion after the file gets uploaded and
it's quite time consuming when there are so many Links and Images to tie together. It just seems that there has to be a better or easier way of doing this.

Any ideas on this one. Where to begin, how to approach it etc...?

2. Is there anyway to get the size information of an image during the upload and store it in along with the servername, filetype and filesize. I notice when
you look at the page info for an image that it will tell you the image dimensions, so somehow that information must be known. Is there a variable one can
use to access it. (Browsers load a page more quickly if the dimensions of the image are known, so this information would of great use)

Thanks for your help.

Quote Reply
Re: Accessing Attachments In reply to

Insert this Mod in HTML_Templates.pm
in the sub site_html_link after the code:
(ref $rec eq 'HASH') or croak....

# Mod Generate Attach tags
# It can slow-down the build time
# be shure to add the global const
# $LINKS{attach_url}="http://www.yourdomain.com/upload/"
# in the Links.pm file
# This Mod generates the following tags
# <%Att1%><%Att2%>...,<%AttPath1%>...,<%AttSize1%>...,<%AttType1%>
my $link_db = new Links: BSQL $LINKS{admin_root_path} . "/defs/Links.def";
my $att_list = $link_db->list_attach ($rec->{'ID'});
my $i =0;
foreach my $attach (@$att_list) {
$i ++;
$rec->{'Att' .$i} = $attach->{FileName};
$rec->{'AttPath'.$i} = $LINKS{attach_url} .$attach->{ServerName};
$rec->{'AttSize'.$i} = $attach->{FileSize};
$rec->{'AttType'.$i} = $attach->{FileType};

# End Mod Generate Attach tags

Try it out and tell me what you think of it.

regards, Alexander
Quote Reply
Re: Accessing Attachments In reply to

The code works great. I've even ported it over to the build_detail in nph-build.cgi and site_html_print_cat in HTML_templates.pm and with the minor change of changing the $rec-> to the appropriate $variable-> I've gotten everything to work.

As far as the slow down, I probably can't really speak to that as of yet. Right now, I can only go by feel and it's telling me that it's a tiny bit slower, but not much. I get the sense that the slowdown is from having to open the db.

Later next week once I get my database loaded with actual data I'll get a better sense of that.

If I find that there is indeed a major sacrifice in performance, I'm thinking to look for a way to add an attachment drop down field to the links and category databases. Then when an attachment gets added to the attach db, it should update the info in the Links db attachment field. Probably very tricky.

One final comment on this. IN DBSQL attach_file - The default installation causes an uploaded attachment to be named ID.fil regardless of what it the attachment is. I found that a new upload would overright the old upload file (this is important if you only want to allow one attachment per Link), yet the attach database record would indicate there were two attachments(I think that's a bug, because you'd delete one attachment thinking the other was still there, but it wasn't because both attachments accessed the same file (i.e. ID.fil) Anyway, I made the following change in DBSQL.pm

sub attach_file {


$fn = $fh;

$fn = $data_id . $fh ;

That simply adds the the actual filename to the ID number, and allows for multiple attachments.

Then change:
VALUES (?, $data_id, "$data_id.fil", $fn, $ft, $size)

VALUES (?, $data_id, $fn, $fn, $ft, $size)

I haven't figured out why both a file name AND a server name were needed nor why the server wanted to call it .fil. I'm still confused on that. And also trying to view an image called 15.fil just gave me a bunch of strange characters. (The browser would load it fine, but I couldn't "view image" from Netscape 4.6

Anyway, because the ID is now added to the filename itself, the following advantages can be spoken of:

1. All distinct records can still have the same named file uploaded to them. Note: You can't upload a file called picture.jpg twice and expect two version to be available to the same ID.

2. Any record can have any number of attachments uploaded (assuming of course the uploaded file is a unique name.)

I haven't tested this out from modify.cgi, or add.cgi yet. I'm assuming that because they use the same sub routine that it will be fine. Anyway. If I run into trouble on that I'll be sure to share it.

Alexander, Thank you again for all your help.



Quote Reply
Re: Accessing Attachments In reply to
There is a reason for it being that way, although it doesn't fit everyone's use, and I can see why you would want your own name.

The reason to have a filename and a servername is so that you don't need to worry what the user sends as the filename. Remember the filename is provided by the user, so before you save it, make sure it is safe. We save the information to FileName so that you can use it later on. A good tip is to use jump.cgi to get the data:


that way when the person saves the file, it's presented with the proper name, but you can still control access to the file.

Again, this is useful in some situations, and a lot of extra headache in others. I am working on improving the file upload ability to make it a little more flexible, and easier to change.


Quote Reply
Re: Accessing Attachments In reply to
Hi Kyle

Have a look at:

It fixes the bug of multiple attachements. You can also change it to ID+FileName and then you can upload any amount of attachements with the name picture.jpg to the same link.

regards, Alexander