• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

Undefined hash reference error using CGI's uploadInfo()

I get an "undefined hash reference error" when I use uploadInfo() in this code. The keys of %vars are  
"input type = file[]":

1 my %vars = $cg->Vars;
2 while(($key,$value) = each %vars)
3 {
4    my $fh = $cg->upload($key);
5    $file_name = $cg->param($key);
6    $file_type = uploadInfo($file_name)->{'Content-Type'};
7 }

Line 6 gives me the error.

Any ideas?
1 Solution
If you print the keys to the %var hash, you should find at least 1 of them will not a filename to be uploaded; it'll be the name of the submit button.  So when you do
$file_type = uploadInfo($file_name)->{'Content-Type'};
on that key, $file_type will be undefined.

Also, the $fh could be undefined for the following reason (taken from the cgi cpan doc),

There are occasionally problems involving parsing the uploaded file. This usually happens when the user presses "Stop" before the upload is finished. In this case, CGI.pm will return undef for the name of the uploaded file and set cgi_error() to the string "400 Bad request (malformed multipart POST)". This error message is designed so that you can incorporate it into a status code to be sent to the browser. Example:

   $file = upload('uploaded_file');
   if (!$file && cgi_error) {
      print header(-status=>cgi_error);
      exit 0;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now