in reply to Getting a file's extension in an upload form

Check out File::Basename's fileparse function.

HTH

Sweetblood

  • Comment on Re: Getting a file's extension in an upload form

Replies are listed 'Best First'.
Re^2: Getting a file's extension in an upload form
by jeffa (Bishop) on Jun 17, 2004 at 20:50 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)