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"'


In reply to Re: Checking File Types by rob_au
in thread Checking File Types by andrew

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.