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

When a user submits a file in an html form the location of the files is sent e.g. "c:\files\help.txt" What I need is this file on the server running the script. How do I do this in my perl script? I guess since I got the file location and I can get the ip but then... I don't know.

Replies are listed 'Best First'.
Re: Saving submitted files
by chromatic (Archbishop) on Mar 02, 2001 at 03:35 UTC
    See uploading a file and related items in Super Search for instructions on how to do this with the CGI.pm module.

    You don't need to know the path on the client or even the IP address of the client. If the client's user agent is working properly, it will encode and POST the file to the server. CGI.pm provides mechanisms to access the uploaded file safely and sanely.

      I think you must have  <form ... enctype="multipart/form-data"> in the HTML form in order to make the client's user agent encode the form, including the file's contents, in the multipart/form-data encoding. And then CGI.pm will help parsing it.
      use CGI; my $q=new CGI; my $file=$q->param('foo'); #foo is the name of the file field in the f +orm print "You uploaded $file:"; #$file is the name of the file print <$file>; #and it can also be used as a file handle to get the co +ntents of the file
Re: Saving submitted files
by footpad (Abbot) on Mar 02, 2001 at 19:54 UTC

    Here's a relevant snippet from the CGI.pm docs:

    To be safe, use the upload() function (new in version 2.47). When called with the name of an upload field, upload() returns a filehandle, or undef if the parameter is not a valid filehandle.

    $fh = $query->upload('uploaded_file'); while (<$fh>) { print; }

    This is the recommended idiom.

    This article shows a practical example.

    Use something like this to check your CGI.pm version:

    % perl -e "use CGI; print CGI->VERSION;"

    Hope this helps...

    --f