Okay, I've been looking high and low for this, and I simply cannot find any solution to my problem...

I am in the process of creating an album review-database, and I need to give the users the ability to upload covers for the reviews.

The problem is, after creating a file upload CGI in a separate script, to test the functionality (this is my first time), and getting it to work, I attempted to turn this into a subroutine. But it didn't quite work.

The problem is, I get an undefined value in hash error with the uploadInfo method (which I can't find documented in CGI.pm, I have to go look around in the manpage). What gives?

#this is how I call the sub: $filename = &uploadcover(param('cover'), upload('cover'), uploadInfo(p +aram('cover'))->{'Content-Type'}); #this is how the sub looks: sub uploadcover { ############################################################## # usage: $filename = &upload($filename, $filehandle, $type); # # will accept the upload of the file, # # and return what filename it was assigned # ############################################################## my ($filename, $filehandle, $type) = ($_[0], $_[1], $_[2]); #if there exists a file with this name, we give the uploaded file +a new name while (-e $filename) { $filename =~ /(\d*)(\w+.\w+)/; $filename = ($1+1)."$2"; } #if this was not a correct filetype, generate an error unless ($type eq "image/jpeg" || $type eq "image/gif") { die "JPEG or GIF files only!"; } #these lines write the filedata to the filename open (COVERFILE, ">$filename") or die "$!"; binmode(COVERFILE); while (<$filehandle>) { print COVERFILE; } close(COVERFILE); #returning the filename for storing in the database return $filename; }

In reply to CGI file uploading subroutine by carthag

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.