Welcome to WebmasterWorld Guest from 54.145.136.73

Forum Moderators: coopster & jatar k & phranque

How to get all <form> values from a multipart form?

The "multipart" bit kills my value-getting routine

   
4:10 am on Apr 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How can my script get the name/value pairs of all the fields in a submitted form, when the script doesn't know ahead of time what the field names are, and when the submitted form has the attribute enctype="multipart/form-data"?

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.

5:13 am on Apr 18, 2009 (gmt 0)

5+ Year Member



The CGI modules documentation covers your questions. $cgi is the CGI object (assumes you are using OO interface):


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.

[perldoc.perl.org ]

2:37 pm on Apr 18, 2009 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Just a side note, your second solution, a semi-standard read/parse (in that it only accepts POST and probably doesn't read GET properly), doesn't work with multipart because it's just splitting form input into key/value pairs. A multipart form sends boundaries that defines the multiple parts. To find the attachments, you need to look for the boundaries.

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.)

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month