Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Problematic UploadFile() method in CGI.pm

by monkfan (Curate)
on May 20, 2006 at 18:09 UTC ( #550719=perlquestion: print w/replies, xml ) Need Help??

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

Most revered monks,

Lately I have been plunging myself into Perl's CGI, programming. And stumbled myself on some aspects of it which I can't seem to figure out the problem. Thus to you I turn for enlightenment....

I was trying to do a file upload through UploadFile() method in CGI.pm. It is done in this way.
# Problematic Upload Entry strong('Choose File to upload: '), filefield(-name=>'upload',-size=>60),br,
However when I tried to capture the result. Although it can identify the file name, but it cannot identify it's value.
my $file = param('upload'); # This line print successfully print h2('File name'), $file; # But not the subsequent lines # Line 55 begins here print h2('File MIME type'), uploadInfo($file)->{'Content-Type'}; while (<$file>) { print; $length += length($_); } print h2('File length'), $length;
The error message given by the problematic line 55 is:
Can't use an undefined value as a HASH reference at /home/ewijaya/publ +ic_html/MyTest/cgi-bin/price_debug.cgi line 55.
Furthere more if you comment out the uploadInfo($file)->{'Content-Type'} line. The content inside while loop doesn't get printed.

To my understanding, I have constructed the uploadInfo() method as according to the doc. But is there anything else that I missed here? The complete code listing can be found here:

#!/usr/bin/perl -w use strict; use CGI qw/:standard :html3/; use CGI::Carp qw( fatalsToBrowser ); print header, start_html('Order Ice Cream with Price'), h1('Order Ice Cream with Price'); generate_form(); print_results(); sub generate_form { print hr, start_form, strong('Your email: '), textfield( -name => 'user_email' ), br, br strong('Cone: '), radio_group( -name => 'cone', -multiple => 1, -values => [qw/sugar waffle/] ), br, br strong('Number of Units: '), textfield( -name => 'no_unit' ), br, br # Problematic Upload Entry strong('Choose File to upload: '), filefield(-name=>'upload',-size=>60),br, + submit( -value => 'Submit' ), end_form, hr; } sub print_results { print "You ordered ", param('no_unit'), ' unit of ', param('cone'), ' cone.'; print br; my $ct = param('cone'); my $nu = param('no_unit'); my $uemail = param('user_email'); my $length; my $file = param('upload'); # This line print successfully print h2('File name'), $file; # But not the subsequent lines print h2('File MIME type'), uploadInfo($file)->{'Content-Type'}; while (<$file>) { print; $length += length($_); } print h2('File length'), $length; print br; }
And it can be tested through this link.

Regards,
Edward

Replies are listed 'Best First'.
Re: Problematic UploadFile() method in CGI.pm
by wfsp (Abbot) on May 20, 2006 at 19:16 UTC
    The cgi docs give this example:
    $fh = upload('uploaded_file'); while (<$fh>) { print; }
    CGI.pm creates a temporary file of the uploaded file. The above method gives you the file handle to that file.

    Hope that helps

Re: Problematic UploadFile() method in CGI.pm
by ioannis (Abbot) on May 20, 2006 at 20:08 UTC
    You need add a parenthesis to capture the hash reference, otherwise you will keep getting undef:

    print h2('File MIME type'), (uploadInfo($file))->{'Content-Type'};

      I don't believe that's correct. See this small example:
      my $hash = { a => 'b' }; sub foo { print $_[0], "\n"; return $hash; } print foo('abcd')->{a};
      No parens required to use the arrow notation with a subroutine return value.
Re: Problematic UploadFile() method in CGI.pm
by gellyfish (Monsignor) on May 21, 2006 at 10:38 UTC

    you need to set the enctype of the form to 'multipart/form-data', CGI provides the start_multipart_form for this purpose which you should use instead of the plain start_form

    /J\

Re: Problematic UploadFile() method in CGI.pm
by ahmad (Hermit) on May 21, 2006 at 10:26 UTC

    Hello

    First you'll need to get the file name to save it , *if you're planning to do so ... actually i cant see that you're saving anything in your code

    # First -- Get file name from our File Box my $file = param('upload'); # Strip out file path from file name , to be able to save it out in ou +r server $file =~ s/.*[\/\\](.*)/$1/; # Next , Generate upload handle my $uph = upload("upload"); #now we save out our file using the name we already got open(OUT,">$file") or die $!; binmode(OUT); while(<$uph>){ print OUT $_; } close(OUT);

    now you should see the same file in your server (uploaded)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://550719]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2022-12-08 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?