If you just want to make sure that the file you store has the same suffix as the uploaded file, then you're on the right track.
However, if you need is to identify the type of file uploaded and act on it accordingly, you might be better off using the mime type than the file extension. clients with macs will be pretty loose with the extensions, for example.
with CGI.pm to handle the uploads - you are using CGI.pm to handle the uploads, aren't you? - then it's easy:
my $filename = $query->param('upload_field');
my $type = $query->uploadInfo($filename)->{'Content-Type'};
Which will give you something like 'image/gif' or 'text/html'. You can derive a file suffix from the second part in a fairly reliable way, but you might need to map from /text to .txt and /jpeg to .jpg, and so on.
For images, by the way, you can also get a very reliable indication of the type from the absurdly useful Image::Size module:
use Image::Size;
my $filename = $query->param('upload_field');
my ($width, $height, $type) = imgsize($filename);
Which will give you a plain 'gif' or 'jpg' or 'png' or 'swf', but doesn't work for anything else.
|