Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm currently retrieving an uploaded file's extension just by splitting the filename split(/\./,$filename). This, however, isn't very efficient as it doesn't take into account that a file might have more than one period in its name. Also, files uploaded from the Windows NT Desktop seem to add some horrible gibberish at the end of the file such as "NTDesktop\etc\etc".

Any ideas on how to retrieve a file's extension?

Thanks,
Ralph

Replies are listed 'Best First'.
Re: Getting a file's extension in an upload form
by sweetblood (Prior) on Jun 17, 2004 at 19:18 UTC

      Good answer, but then what? fileparse has to have a list of acceptable extensions before it will strip them. So if you don't know what extensions to use ahead of time, File::Basename is not going to be much help at all. Try the following one-liners to see what i mean.

      perl -MFile::Basename -le"print for fileparse('/path/to/foo.txt.bak')" perl -MFile::Basename -le"print for fileparse('/path/to/foo.txt.bak',' +.bak')" perl -MFile::Basename -le"print for fileparse('/path/to/foo.txt.bak',' +.txt')" perl -MFile::Basename -le"print for fileparse('/path/to/foo.txt.bak',' +.txt.bak')"
      If you know of a generic regex to pass to fileparse, i would really love to know what it is. But, the problem of knowing just which extension to pull out of foo.bar.baz.qux is tough with or without File::Basename (which i think is a fantabulous module, BTW). That is just something that the coder has to determine ahead of time ... probably a good thing considering the dangers of file uploading.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re:Getting a file's extension in an upload form
by pbeckingham (Parson) on Jun 17, 2004 at 20:00 UTC

    If you are willing to accept the dubious definition of an extension being the characters after the last period, then the following will work:

    my $ext = ''; $ext = $1 if $file =~ /\.(.*?)$/;

Re: Getting a file's extension in an upload form
by Anonymous Monk on Jun 17, 2004 at 20:55 UTC

    I've come up with a dirty yet fast solution to this problem. This should get the file's extension (considering whatever is after the last period) and also get rid of any junk that Windows NT appends to the filename when it's being pulled from the Desktop.

    @extension = split(/\./,$filename); $extension = $extension[$#extension]; @extension = split(/\\/,$extension); $extension = $extension[0];

    So if $filename is something like "C:\Whatever\some horrible.filename.by.me.doc\\NTDesktop\whatever", this will return "doc".

    - Ralph

      Condensing this into a single line, ... (just for fun ;o) )
      $extension = (split(/\\/,(split(/\./,$filename))[-1]))[0]; print "$extension\n";
      or the, inevitable, map expression ...
      ($extension) = map{ split /\\/,$_,2 } (split(/\./,$filename))[-1]; print "$extension\n";

      PJ
      unspoken but ever present -- use strict; use warnings; use diagnostics; (as needed)