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

I've never done this before and was a little stunned at paucity of information I found googling.
edit: hey, I just found the solution googling...

Anyway, the form looks like this:
<form name="test" method="post" action="/cgi-bin/test.pl"> <input name="myfile" type="file"> <button>Submit</button> </form>
edit: the form should be enctype="multipart/form-data"

And following the CGI.pm documentation at CPAN, I've tried this:
my $cgi = new CGI; my $file = $cgi->param('myfile'); my $fh = $cgi->upload('myfile'); #my $type = $cgi->uploadInfo($file)->{'Content-Type'}; print "<p>$file"; print $_ while (<$fh>);
edit: all this is fine now
the "uploadInfo" line is commented out because it causes an error ("Can't use an undefined value as a HASH reference"). $file returns the correct filename, but contrary to the CPAN documentation, this does not have a path attached. And $fh is always undef, even if it is the only request, and (contrary to the CPAN documentation) $file does not function as a file handle.

edit: I guess there was nothing too contrary about the CPAN docs after all...except $file really does not have a path
:)

Replies are listed 'Best First'.
Re: uploading via CGI
by ikegami (Patriarch) on Jul 17, 2009 at 18:43 UTC

    You need to use enctype="multipart/form-data" on your form. Files can't be sent using the default encoding, application/x-www-form-urlencoded

    contrary to the CPAN documentation, this does not have a path attached.

    I don't know where you see that. For at least five years, CGI has been saying:

    Different browsers will return slightly different things for the name. Some browsers return the filename only. Others return the full path to the file, using the path conventions of the user's machine. Regardless, the name returned is always the name of the file on the user's machine, and is unrelated to the name of the temporary file that CGI.pm creates during upload spooling (see below).

      thanks ikegami, if I hadn't finally noticed that in the W3C docs you would have saved me some more grunting and hammering on things ;)

      "I don't know where you see that. For at least five years, CGI has been saying:"
      True. I was flipping back and forth between that and this:
      http://www.perlfect.com/articles/upload.shtml
      Silly me...all apologies to Mr. Stein.

        It's also stated in CGI's docs:

        In order to take full advantage of this you must use the new multipart encoding scheme for the form

        The word "full" should be removed, but the emphasis is in the original.