I am using CGI.pm and am allowing a user to upload a file. I would like to detect different scenarios and display an appropriate error message when invalid inputs are specified by the user. For example if the user selects a directory to be uploaded I would like to detect this and say 'Sorry, you've chosen a directory. This is an invalid file.'. If the user chooses a zero byte file then I'd like to say, 'Sorry, you have selected an empty file for upload.' My problem is I don't know how to tell the difference between a directory, a zero byte file and a non-existant file. I read somewhere that you should be able to use the -d operator with a file handle if you want. So, I tried this but it didn't work! I typed c:\oracle in my file browse box (the name of a directory on my computer) and submitted to my CGI Perl script.

my $err=""; my $inputFileHandle = $cgi->param("FILE"); if ( -d <$inputFileHandle> ) { $err.="This is a directory.<br/>"; } if ( !-f <$inputFileHandle> ) { $err.="This is not a plain file.<br/>"; } if ( -z <$inputFileHandle> ) { $err.="This is a zero byte file.<br/>"; } if ( !-e <$inputFileHandle> ) { $err.="This is a non-existing file.<br/>"; }
These flag operators didn't work as I expected. !-e and !-f actually got entered every time and the -d and -z blocks were never entered regardless of whether the file was valid, empty, non-existing or a directory.

Is there another way I can detect more information about what a CGI upload file handle points to with Perl?

Thanks,
  Will

In reply to Error detection on CGI file upload attempts by willjones

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.