Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Uploading a File with CGI.pm

by dru145 (Friar)
on Apr 11, 2002 at 19:14 UTC ( [id://158396]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to upload a file to a website via CGI.pm to be processed, but I can't get it to work. I followed the documentation and the advice I found on some older posts. I would appreciate if someone can tell me what I'm doing wrong:

Portion of form that asks for the file to upload
Tr( { -style => "background-color:#CCCCCC" }, td( strong( "Enter the file you want to upload" ) ), td( filefield( { -name=>'file', -default=>'starting value', -size=>30, -maxlength=>80 } ) ), #end td ), #end Tr


Script that processes the file
#!/usr/bin/perl -w use strict; use CGI::Carp 'fatalsToBrowser'; use CGI qw/:standard/; my @lines; my $filehandle = upload('file') or die "No file uploaded!"; my $message1 = "<u>The Following Data was inserted into the Database < +/u>"; do_work(); display_page("$message1"); sub do_work { while (<$filehandle>){ push (@lines, $_); } } sub display_page { my $message = "$_[0]"; print header, start_html( "-title" => "Results Page"), p("&nbsp;"), p( strong( $message ) ), p("&nbsp;"), p("@lines"), end_html; }


I keep getting the error message "No File Uploaded!"

Thanks,
Dru
Another satisfied monk.

Replies are listed 'Best First'.
(ichi) Re: Uploading a File with CGI.pm
by ichimunki (Priest) on Apr 11, 2002 at 19:23 UTC
    According to the CGI perldoc you need to make sure to start your form code with start_multipart_form... are you doing that? Also it should be my $filehandle = param('file') or die "$!"; according to the same POD.

      According to the CGI perldoc you need to make sure to start your form code with start_multipart_form...

      I must have missed that. I had start_form I changed it to start_multipart_form and now it works!

      Thanks,
      Dru
      Another satisfied monk.
Re: Uploading a File with CGI.pm
by tachyon (Chancellor) on Apr 11, 2002 at 19:29 UTC

    If your version of CGI.pm is not up to date then the upload() method will be broken or non existant. 2.47 rings a bell. Here is some code that I have used before that does work!

    use CGI; # allow uploads $CGI::DISABLE_UPLOADS = 0; $CGI::POST_MAX = 1,024,000; my $q = new CGI; ..... sub upload_file { no strict 'refs'; # CGI < v2.47 use vvvvvv a symbolic reference f +or the filehandle my $fh = $q->upload('upfile') || $q->param('upfile');; my $buffer; open (OUTFILE, ">$path_to_files/$upname") or die_nice("Unable to create upload output file '$upname': $! +\n"); binmode $fh; binmode OUTFILE; my ($bytes, $read); while ($read = read ($fh, $buffer, 16384)) { print OUTFILE $buffer; $bytes += $read; } close OUTFILE; $bytes /= 1000; return "File '$upname' uploaded. $bytes Kb received successfully.<br +>\n"; }

    The main point is that you can use the return value of $q->param('upfile') as a symbolic reference to get a filehandle. Indeed you have to in early version of CGI.pm, thus the or clause. Also note that although CGI.pm allows file uploads by default a wise admin may hack source so that they are denied by default so you should make sure they are enabled. You need to make sure that your submitting form uses the correct ENCTYPE (multipart/form-data) note that the CGI.pm default is "application/x-www-form-urlencoded" so you need to use start__multipart_form so you get a form like:

    <FORM METHOD="POST" ACTION="http://somewhere.com/cgi-bin/script.cgi"; ENCTYPE="multipart/form-data"> <INPUT TYPE="file" NAME="upload_file" SIZE="42"> </FORM>

    PS You can check your version of CGI.pm like this:

    use CGI; print CGI:VERSION;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-18 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found