Gossamer Forum
Home : General : Perl Programming :

extract file-extension

Quote Reply
extract file-extension
Hi,

I would like to have the following routine save the attachment under "username.ext"

.ext should have to be extracted from $in{'FILE_NAME'}. But how?

The following 2 lines extract name.ext from c:/dir/subdir/name.ext, but I only need .ext!
Code:
my $filename = $in{'FILE_NAME'};
($filename =~ m,[/\\]([^/\\] )$,) and ($filename = $1);


My whole sub: (If your curious)

Code:
# Save any attachments
if ($in{'FILE_CONTENT'}) {
if (length($in{'FILE_CONTENT'}) > 300000) {
&site_html_request_error ("printenv: Maximum attachment size is 300kb. Please email with attachment.");
return;
}
if ($in{'FILE_NAME'} !~ /\.doc$|\.rtf$|\.txt$/i) {
&site_html_request_error ("printenv: Only files with the following extension(s) are allowed: .doc, .rtf and .txt");
return;
}
my $file_id = $in{$db_key};
my $attachment_name = $in{$db_cols[$db_attachment_name]};

$attachment_name =~ s/ä/ae/g;
$attachment_name =~ s/ö/oe/g;
$attachment_name =~ s/ü/ue/g;
$attachment_name =~ s/ë/e/g;

open (FILE, ">/users/www/data/attachments/$file_id-$attachment_name") or &cgierr ("Can't save attach: $file_id-$attachment_name. Reason: $!");
print FILE $in{'FILE_CONTENT'};
close FILE;

}
Quote Reply
Re: extract file-extension In reply to
 I'd do this,

$filename = "$in{'FILE_NAME'}";
$filename =~ s!.*\.(.*)!$1!g;
$filename = ".$1";

I tested it out so I know it will work for you.

perlkid

Quote Reply
Re: extract file-extension In reply to
umm.. no.. that is strange way of doing it.

($ext) = $in{'FILE_NAME'} =~ /(\.[^\.]+)$/;

if the filename is.. blah.txt it returns.. '.txt'.. if its .htaccess.. it returns '.htaccess'

if its.. file.. it doesn't return anything..

for no period at the beginning of the extension..

($ext) = $in{'FILE_NAME'} =~ /([^\.]+)$/;

Jerry Su
Quote Reply
Re: extract file-extension In reply to
Thanks,

I tried the following, but It does NOT work.

I get "1" for $extension if I try to upload a .doc file!


Code:
my $extension = $in{'FILE_NAME'} =~ /(\.[^\.] )$/;
my $file_name = $in{$db_key} . $extension;

if ($file_name !~ /\.doc$|\.rtf$|\.txt$/i) {
&site_html_request_error (Only files with the following extension(s) are allowed: .doc, .rtf and .txt");
return;
}
Quote Reply
Re: extract file-extension In reply to
correct, you cannot do

($ext) = $in{'FILE_NAME'} =~ /([^\.]+)$/;

You will get 1 if it is successful and nothing if it isn't.

What you want is:

$in{'FILE_NAME'} =~ /([^.]+)$/;
$ext = $1;

--mark

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: extract file-extension In reply to
Sorry Mark, you can. =)

Regex's return a list of matches as result, so the 1 was because it was in scalar context. If he had brackets around it, it would have worked. =)
(Also, there was a space at then end instead of a plus for some reason).

Replace:

my $extension = $in{'FILE_NAME'} =~ /(\.[^\.] )$/;

with:

my ($extension) = $in{'FILE_NAME'} =~ /(\.[^\.]+)$/;

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: extract file-extension In reply to
errr, ok guess it's time that i revisit something that I "learned" 3 years ago...

That's what happens when you try something when learning perl, and for whatever reason, never revisit it when you KNOW Perl LOL.

Years ago I attempted it, it didn't work, and I've been on the $1 since. Of course, NOW trying it works. Sure would like to know what the hell I did years ago that DIDN'T work...must have also been in scalar context. Arrrgh.

*mental note: concepts from a few years ago, need to be retested nowadays...*

*sigh*

K, ignore my previous post. (Gordon, stop laughing Smile)

--mark

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: extract file-extension In reply to
should be..

Code:
my ($extension) = $in{'FILE_NAME'} =~ /([^\.]+)$/;
why'd you change mine....? :)


mark..

we all do make mistakes.. ;)

i'll try to make Gordon say something wrong so you can have your chance at laughing at him.. hehe


Jerry Su
Quote Reply
Re: extract file-extension In reply to
Bah, gordon and I share an office. we spend all day laughing at each other Smile

--mark

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.