in reply to How do I parse multipart/form-data?!?

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; }

Replies are listed 'Best First'.
RE: Re: How do I parse multipart/form-data?!?
by PipTigger (Hermit) on Jun 08, 2000 at 05:08 UTC
    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.