Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 12, 2003
votes: 0

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)

Junior Member

5+ Year Member

joined:Dec 20, 2008
votes: 0

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)

Senior Member

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

joined:Nov 28, 2004
votes: 0

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


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members