carthag has asked for the wisdom of the Perl Monks concerning the following question:

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; }

Replies are listed 'Best First'.
Re: CGI file uploading subroutine
by dws (Chancellor) on Nov 23, 2001 at 09:10 UTC
    If this is where you're getting into trouble,   uploadInfo(param('cover'))->{'Content-Type'} then try extracing Content-Type before you pass the result as a parameter. I do something like:
    my $filename = param('filename'); my $info = uploadInfo($filename); my $type = $info->{'Content-Type'};
    and have had no problems.