homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

How to get all <form> values from a multipart form?
The "multipart" bit kills my value-getting routine

WebmasterWorld Senior Member 10+ Year Member

Msg#: 3895066 posted 4:10 am on Apr 18, 2009 (gmt 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+ Year Member

Msg#: 3895066 posted 5:13 am on Apr 18, 2009 (gmt 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 ]


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

Msg#: 3895066 posted 2:37 pm on Apr 18, 2009 (gmt 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.)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved