Gossamer Forum
Quote Reply
file extention global
hello,

is there a global that checks the extention of a attached file and eventually display it:

example:

<%if Extention eq '.jpg'%>
<IMG SRC="<%db_cgi_url%>/jump.cgi?view=File&ID=<%ID%>">
<%endif%>


in this case it should check the attachment, and if it's a .jpg file, display it.
Quote Reply
Re: file extention global In reply to
Hi,

You could do:

<%if FileColumn like '.jpg'%>
..
<%endif%>

as the column stores the actual name.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] file extention global In reply to
I have just tried out the upload-feature.
I have added five new columns with name File1 to File5
for every field i give the same path to store the files:
/home/lalala/blabla/img

Then i took the first link and pass three files;
then the second link with 5 files

On my server the files are stored now:
.../img/1/1-name.jpg
.../img/2/2-name.jpg
...
.../img/8/8-name.jpg


In my links-table i have the information:
field File0 name.jpg
field File1 nameb.jpg


So to show a picture i must select from table Links_Files
select id,File_Name where ForeignColName = Field and ForeignColKey = LinkID

with this i must write img src = URL/ID/ID-File_Name


Why this ?

I would understand if the files are separated by links, but for every single one a single dir ???

Robert

Quote Reply
Re: [Robert] file extention global In reply to
Ok, i have fixed the thing and write the following global:
Name: FileShow1 for field File1

sub {
my ($rec) = @_;
my $id = $rec->{ID};
my $db = $DB->table ('Links_Files');
my $dummy;
my $sth = $db->select ( { 'ForeignColName' => "File1", 'ForeignColKey'=>$id}, ['id', 'File_Name'] );
while (my ($file_id,$file_name) = $sth->fetchrow_array) {
$dummy = "<img src=\"/imgup/$file_id/$file_id-$file_name\">";
}
return $dummy;
}


Its an easy query asking for fileid and filename for the row with Columname and LinkkID
But i still dont understand why every file is saved in his own directory ?!


ToDo:
if, else ... for images, pdf, doc, txt ... later.

Robert
Quote Reply
Re: [Robert] file extention global In reply to
Ok, it is not ready to run, but i hope someone could do the rest. (if not, ask me and i will look for the mime-types and the regex)

sub {
my ($rec) = @_;
my $id = $rec->{ID};
my $db = $DB->table ('Links_Files');
my $dummy;
my $sth = $db->select ( { 'ForeignColName' => "File2", 'ForeignColKey'=>$id}, ['id', 'File_Name','File_MimeType'] );
while (my ($file_id,$file_name, file_mime) = $sth->fetchrow_array) {

if ($file_mime ~ regex for image/gif or jpeg){
$dummy = "<img src=\"/imgup/$file_id/$file_id-$file_name\">";
}
elsif ($file_mime ~ regex for pdf){
$dummy = "<a href=\"/imgup/$file_id/$file_id-$file_name\">PDF</a>";
}
}
return $dummy;
}



The more i work with 2.1, the more i love it. Great job, Alex.
Robert

Last edited by:

Robert: Feb 12, 2002, 3:21 AM
Quote Reply
Re: [Robert] file extention global In reply to
Instead of escaping "s the code would be clearer using something like qq| |

eg...

$dummy = qq|<img src="/imgup/$file_id/$file_id-$file_name">|;

Also I notice the code is within a while loop so you'd need .= rather than = if you are wanting to build up the output.

Last edited by:

RedRum: Feb 12, 2002, 3:33 AM
Quote Reply
Re: [RedRum] file extention global In reply to
Sorry, im no programmer at all. But qq| ... seems logical.
The other thing i dont quite understand; i always get only one query, so maybe it could be done shorter.
But .= is not needed i think.
Robert
Quote Reply
Re: [Robert] file extention global In reply to
As reading ur text the second time i got another idea:
I could ask for all pics with an for next (File1, File2 ... File5)
and parse the output with .= as you write.
But i think i will need the pics on different catpages at different places.
Robert
Quote Reply
Re: [Robert] file extention global In reply to
Its always better to use the .= in a while. If there are multiplease answers then it will need to add them. = only overwrites the old variable and assigns a new one, whilst .= actually keeps the old one and just adds the extra one on at the end Tongue

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: [Robert] file extention global In reply to
Hi Robert,

I see what you mean. I misread and thought the while loop was doing multiple loops so, = is fine.

As for this bit:

regex for image/gif or jpeg

regex for pdf

...the regex would be:

=~ /\.(gif|jpe?g)$/i

and

=~ /\.pdf$/i

Last edited by:

RedRum: Feb 12, 2002, 8:45 AM
Quote Reply
Re: [Robert] file extention global In reply to
Hi Robert,

The file_info() method is what you are looking for. You want to use GT::SQL::File for this (you can read the docs under the Developers Guide). You could do:

Code:
sub {
my $tags = shift;
my $links_db = $DB->table('Links');
my $fh = $links_db->file_info( 'UploadColumn', $tags->{ID} );

# $fh is a file handle that you can read from to access the file, as well as get
# following information:
#
# $fh->File_Name the basic filename
# $fh->File_Directory path to the file
# $fh->File_MimeType mimetype of the file
# $fh->File_Size size of the file
# $fh->File_RelativePath the permuted file and directory without root
#

my $location = $fh->File_RelativePath;
if ($fh->File_Name =~ /\.(jpg|gif)$/) {
return qq~ <img src="/images/imgup/$location"> ~;
}
else {
return "Invalid file name.";
}
}


This isn't tested, but looks like it should work. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] file extention global In reply to
Thank you, Alex. I will test it tomorrow. But please tell me,
why the files are saved in single directories?
Robert
Quote Reply
Re: [Robert] file extention global In reply to
Hi,

When you add your column you can choose simple or hashed file name. This is because most linux o/s have a limit of 20-30k files per directory, and if you have a very large image database, Links SQL will hash the files (i.e. split them into 10 directories).

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] file extention global In reply to
But where is the information stored in which Dir the file is?
For every image i have saved the path:
/home/.../www/.../domaine.com/imgup

but not
/home/.../www/.../domaine.com/imgup/0 <--from (0-9)

If this information is not stored here, then the field makes no sense?
If the info should be saved here, everything would be clear,
but so i dont understand it.

Have tried your sub just a minute ago, but it seems something is wrong.
Got:
Can't call method "File_RelativePath" on an undefined value at (eval 19) line 16.

In the docu a table is named: parent_table_name_File' ?!
Maybe this was the further name for table Links_Files ?

If i choose simple instead hashed for the file-field, could i save then all my pics in one dir?
I dont know anything about this restriction you write?!
Under my linux i have really a lot of streams in one single dir and never got any problems (about 500megs)

Robert

Last edited by:

Robert: Feb 12, 2002, 10:33 PM
Quote Reply
Re: [Robert] file extention global In reply to
Ok; this worked for me:

Code:

sub {
my ($rec) = @_;
my $id = $rec->{ID};

if ($rec->{File1}) {
my $links_db = $DB->table('Links');
my $fh = $links_db->file_info( 'File1', $id );
my $location = $fh->File_RelativePath;
if ($fh->File_Name =~ /\.(jpg|gif)$/) {
return qq~ <img src="/imgup$location"> ~;
}
else {
return "Invalid file name.";
}
}
else {return "";}
}


Robert
Quote Reply
Re: [Robert] file extention global In reply to
In Reply To:
Ok; this worked for me:

Code:

sub {
my ($rec) = @_;
my $id = $rec->{ID};

if ($rec->{File1}) {
my $links_db = $DB->table('Links');
my $fh = $links_db->file_info( 'File1', $id );
my $location = $fh->File_RelativePath;
if ($fh->File_Name =~ /\.(jpg|gif)$/) {
return qq~ <img src="/imgup$location"> ~;
}
else {
return "Invalid file name.";
}
}
else {return "";}
}


Robert