Undefined hash reference error using CGI's uploadInfo()

Posted on 2006-05-10
Last Modified: 2011-09-20
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?
Question by:tr5
    1 Comment
    LVL 28

    Accepted 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, 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;

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now