#this is how I call the sub: $filename = &uploadcover(param('cover'), upload('cover'), uploadInfo(param('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; }