I'm trying to build an upload script that only allows certain file types (doc, docx, odt, & rtf). The only problem is, the script uploads any file I give it, even if it's the wrong type.

Cutting out the irrelevant code, I have:

#!/usr/bin/perl -wT use strict; use CGI qw/param uploadInfo/; use CGI::Carp qw ( fatalsToBrowser ); use File::Basename; my $query = new CGI; my $filename = $query->param("manuscript"); my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); $filename = $name . $extension; my @allowedext = qw(doc docx odt rtf); unless(@allowedext = $extension) { print $query->header ( ); print "Filetype not allowed. Please upload doc, docx, odt, or rtf fil +es only."; exit; }

Any ideas what I'm doing wrong?

Update: Oops, I guess I stripped out too much! In the full script, $name and $extension are initialized. Updated the code above to include it. The unless($allowedext{$extension}) was someone else's suggestion. It was originally unless($extension = @allowedext) I've changed and overwritten so much, I forgot that that piece of code didn't work. When running the script successfully, but without the extension check stopping the upload of an unacceptable filetype, I had resorted to the original piece of code, which is now reflected above.

Update 2: It ain't pretty, but I got it working. I still don't know what I couldn't get map to work on my webhost, but this gets around that issue.

if (lc $extension ne ("doc" "docx" or "odt" or "rtf")) { print "Filetype '$extension' not allowed. File extension must be: do +c, docx, odt, or rtf"; exit; }

In reply to extension check on upload script doesn't do anything by edwardra3

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.