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

Greetings!

I know to set a type=text form field parameter in CGI.pm you can do it like so:

my $cgi = new CGI; $cgi->param("my_var" => "hello there");
But does anyone know how to go about setting a type=file?

In CGI.pm if I access a type=file param in scalar context I get a file name, but if I treat it as a file handle I get the contents of the file. For example:

## Scalar context returns filename my $filename = $cgi->param("upfile"); ## Or... as a filehandle while (<$filename>) { ## do something with contents }

I want to set those parameters (filename and contents) so they can be accessed in the same way later. Any ideas? Thanks!

Replies are listed 'Best First'.
Re: Setting a type=file parameter in CGI.pm
by kyle (Abbot) on Jan 03, 2009 at 05:38 UTC

    I haven't tried this to see if it works.

    According to the CGI documentation, you can create an object from an input file.

    open my $fh, '<', 'saved-cgi' or die "Can't read saved-cgi: $!"; my $cgi = CGI->new( $fh );

    It also says you can create the file it reads with the ->save() method.

    So perhaps you can make a new CGI program that does nothing but take a file upload and save the resulting CGI object. Later, load it back in.

    Again, I haven't tried this to see that it works, but the documentation seems to imply it would.

Re: Setting a type=file parameter in CGI.pm
by hangon (Deacon) on Jan 03, 2009 at 00:36 UTC

    Its not very clear what you are asking. Do you want to preset the form field so you don't have to navigate to the file with the browse button? If so, CGI.pm has no control over this. HTTP does not allow you to preset the file upload field. This is for security purposes.

Re: Setting a type=file parameter in CGI.pm
by Anonymous Monk on Jan 02, 2009 at 20:59 UTC

        Why do you want to set the field after it has been submitted? You can set a new value. The uploaded files are treated special by CGI.pm and I don't think you can set those (and I don't see a reason why you would want to).