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

I have an HTML form which includes a <INPUT TYPE=file ......>, on submission the form calls a CGI. What I want to do in the CGI is save the file to a location. How do I do this? Another (related) problem I have is saving a key value as a text file. I know how to open file handles and use them, but only for files that already exist. What if you want to create a new file and write to it. Thanks for the help.

Replies are listed 'Best First'.
Re: Handling the POSTing of a file to a CGI script
by btrott (Parson) on Feb 18, 2000 at 23:31 UTC
    Use CGI.pm:
    use CGI; my $query = new CGI; my $fh = $query->upload('uploaded_file'); open OUT, ">/home/foo/bar/baz.html" or die "Can't open: $!" while (<$fh>) { print OUT; } close OUT;
Re: Handling the POSTing of a file to a CGI script
by Anonymous Monk on May 31, 2000 at 01:05 UTC
    I tried what btrott put on his/her messages and it didn't work. Later I found on the CGI.pm documentation in CPAN that it's "my $fh = $query->param('uploaded_file');", not "my $fh = $query->upload('uploaded_file');"
Re: Handling the POSTing of a file to a CGI script
by nickcave25 (Acolyte) on Feb 18, 2000 at 22:03 UTC
    open (LOGHANDLE, ">>outputfile.txt")|| &ErrorMessage; flock (LOGHANDLE, 2); print LOGHANDLE "$theDataYouWantToOuputToTheFile\n"; flock (LOGHANDLE, 8); close (LOGHANDLE);
    That is the old way to do it.... The Perl5 way has you
    use Fcntl qw(:flock); #Imports LOCK_EX, LOCK_SH, LOCK_NB
    Can somebody show this guy how to do it the Perl 5 way? I'm not really up on it yet.
      http://www.cgi101.com/class/ch17/upload.txt example
Re: Handling the POSTing of a file to a CGI script
by Anonymous Monk on Feb 18, 2000 at 20:14 UTC
    To your first Question: I have to do the same thing. I have not investigated about this yet, but I will tell to you when I done. The second Q: To write a file, simply open it in Write mode! :) Don't remember exactly, but something like: open(FILEHANDLE,">Filename"); Then, to print to it: print FILEHANDLE, $Variable remember to close FILEHANDLE; !:)