Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How do I parse multipart/form-data?!?

by PipTigger (Hermit)
on Jun 08, 2000 at 02:45 UTC ( [id://16978]=perlquestion: print w/replies, xml ) Need Help??

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

I've got an input type=file with the browse button sending to a form enctype=multipart/form-data! Now what?

I've looked in HTML (tdg), Perl Cookbook, CGI Programming (otwww), Programming Perl, & Advanced Perl Programming in that order. I've nearly exhausted my ora resources and haven't found squat!

Can CGI.pm suck uploaded files into a scalar or something? Does nobody ever do this that it isn't in any of the books where it would be looked for first? What's the best way to upload files to a server? Easiest? Safest?

I'd appreciate help greatly. TTFN & Shalom.

-PipTigger

Replies are listed 'Best First'.
Re: How do I parse multipart/form-data?!?
by chromatic (Archbishop) on Jun 08, 2000 at 03:02 UTC
    There are a couple of good CGI.pm methods to use. Of course, you'll have HTML looking something like this:
    <form action="/cgi-bin/upload.cgi" method="POST" enctype="multipart/fo +rm-data"> File to upload: <input type="file" name="file"> </form>
    The other important thing to note is that CGI::upload() will return a filehandle you can read the file from:
    my $file = $q->param('file'); my $fh = $q->upload($file); my $buffer; open(OUTPUT, $output_filename) || warn "Can't create local file: $!"; # just in case you're not Unix binmode $fh; binmode OUTPUT; while ( read($fh, $buffer, 16384)) { print OUTPUT $buffer; }
      Thanks for the assistance! I've tried to fiddle with my code and this is what I have ($dat is the upload filename):
      $udt = $que->upload($dat); open PAGE, ">$pgp$pgn"; # while (<$udt>) { print PAGE $_; } while (read($udt, $buf, 16384)) { print PAGE $buf; } close PAGE;
      It doesn't get past the upload line. I've checked other posts so I tried to just run $que->uploadinfo; to see if It would return anything and it kills the whole thing too. The only difference I saw (after much perusal) between our code was the use line where I have use CGI qw(:standard); so I tried to change it to just use CGI; like you have and it failed immediately. What's the difference between the use parameter options and is this the reason why my upload() kills the script? What do I have to do to be able to just use CGI;? Thanks again for any further help. This is way harder than I thought it would be. TTFN & Shalom.

      -PipTigger

      p.s. My bad! I just got it to werk! I love lerning new Perl! =) The problem was I didn't need upload at all. This werks great:
      #!/usr/bin/perl use CGI qw(:standard); my $dat = param("upfilenm"); my $pgn = param("pagename"); open PAGE, ">/usr/local/pagez/$pgn"; while (<$dat>) { print PAGE $_; } close PAGE;
      NiceNice!
        upload() is a newer method. If you're using a CGI.pm version earlier than 2.47 (say, from a standard install of Perl 5.005_3 or previous) you won't have it.

        You can still retrieve the filehandle using param().... oh, as I see you found out. There is a danger that on failed uploads param() will return a string only (one that isn't a filehandle) so your reads will fail. Upgrade to the current CGI.pm (if you can) and use upload() to be safe.

        If you use standard when importing CGI, it'll import the functions into your name space, so you can probably get by with: $udt = upload($dat); I presume you're not using the object-oriented interface, but that's just a guess.
Re: How do I parse multipart/form-data?!?
by plaid (Chaplain) on Jun 08, 2000 at 03:04 UTC
    Check out this link, to an earlier discussion on perlmonks. It has a piece of sample code using CGI.pm to get a file upload, and also has a link to the rfc which describes how multipart-form data looks internally.
Re: How do I parse multipart/form-data?!?
by dempa (Friar) on Jun 08, 2000 at 02:52 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found