in reply to Checking File Types

It seems that you are making more work for yourself than you rightly need to. It is for just this type of scenario that I wrote the CGI::Upload module - This module allows you to check file upload type by both contents (using File::MMagic and MIME magic numbers ) and file extension.

For example, using the CGI::Upload module, this code could be reduced to the following (comments included for explanation) :

use CGI; use CGI::Upload; my $cgi = CGI->new; my $upload = CGI::Upload->new; # The array @types contains a list of allowed file # extensions - Note that testing a file by MIME type is # more secure than depending upon the file extension # alone. my @types = qw/ gif jpg jpeg jpe jfif /; # Loop through each of the image upload fields. The # image field names are being used here in place of the # loop index - This has been written thus so that # additional image upload fields, which may not # necessarily be named 'im<num>' can be added. foreach ( qw/ im1 im2 im3 im4 im5 / ) { # Ensure that the CGI parameter has been defined if ( $cgi->param($_) ) { # Check the file type of the uploaded file with the list # of allowed file types in @types (grep works nicely # for this) - If the image file extension is not within # the array @types then return an error to the user. unless ( grep /\Q$upload->file_type($_)\E/ @types ) { error('Your image needs to be in GIF or JPEG format'); } } }

 

perl -e 'print+unpack("N",pack("B32","00000000000000000000000111001011")),"\n"'