in reply to Checking File Types
How do you know that the user is not going to rename whatever file they want to use to have an acceptable file extension? Why not use image::info? It can tell you the mime type or suggested extension for an image.
Untested code snippet:
Output produced:use Image::Info qw(image_info); my $filename = "file5.jpg"; # actually a PNG my $info = image_info($filename); if (my $error = $info->{error}) { die "Can't parse image info: $error\n"; } my $mime_type = $info->{file_media_type}; my $suggested_ext = $info->{file_ext}; print "mime type: $mime_type\n"; print "suggested extension: $suggested_ext\n";
Then you can check against your list of good extensions.
Update: Code updated and tested!
|
---|