Here are the details:
I have a script that receives input from a multipart form, with a potentially-uploaded file as well as regular form fields. I know of two ways to get form field values:
use CGI qw(:standard)
$var = param(fieldName);
However, this requires that the script knows the names of all the fields passed to it, but it doesn't. (PLEASE just trust me that it has to be this way, without arguing that I should design so the script knows what to expect.)
The second way I know is to get all the name/value pairs with a loop:
read(STDIN, $formData, $ENV{'CONTENT_LENGTH'});
@pairs = split('&', $formData);
for $pair (@pairs) {
($fieldName, $value) = split(/=/, $pair); # Split into name & value.
$$fieldName = $value; # Assign value to scalar matching name.
$$fieldName =~ s/%(..)/chr(hex($1))/ge; # Decode encoded stuff.
$$fieldName =~ s/\+/ /g; # substitute +'s for spaces.
}
However, this doesn't work if the form I'm submitting has the attribute enctype="multipart/form-data".
So I'm stuck. I thought I was clever by writing some JavaScript to write the field names to a hidden field before submitting (and my script knows the name of that one field), but the problem with that is if the user submits and then uses the Back button, the contents of my hidden field is empty, and then on the second submit the script doesn't receive a list of fields.
my @parameters = $cgi->param; #gets the names of all form fields
my %params = $cgi->Vars; #stores all form fields as name/value pairs
in a hash
Read the modules documentation for much more detail. I know its long but it is pretty thorough.
So for example, an input type="file" with the name "photo" will give you "photo=path/to/photo.jpg" but the photo is actually an attachment which begins after the boundary. The CGI module is great for this (and is one of the few I like to use regularly.)