cannyboy
cannyboy

Reputation: 24426

Multipart form data parsing in PHP

Forgive my ignorance in this..

I think I understand that using:

extract($_REQUEST);

in a php file will convert html form inputs into a corresponding variable. For instance:

<input type="text"name="author"/>

becomes:

$author

Is that right?

However, I'm confused as to how to handle a multipart form. For instance one with 1 file (an image) and two text inputs. How do I extract this data and put it into variables?

Upvotes: 2

Views: 2281

Answers (2)

mario
mario

Reputation: 145482

Okay, after another reading, your undeerstanding of extract is right. But note that the author input will generally be available as $_REQUEST["author"] anyway. Generally avoid to extract them all.

If you for example want the value to be reused as form input you can write:

<input name="author" value="<?=htmlspecialchars($_REQUEST["author"])?>">

If you want to have shortnames, if for example it's too many fields and it spares lots of typing, then I'd recommend using the optional parameters to extract():

extract($_REQUEST, EXTR_PREFIX_ALL, "i_");

This would generate a $i_author variable, and all other input fields with a $i_ prefix. This is believed to have less sideeffects with other (hyopthetical) local or global variables. Also you can use array_map("htmlspecialchars",$_REQUEST) for extraction, if that aids the processing.


Multipart form data will fill up the $_FILES array additionally, which has a different structure. See PHP manual http://php.net/manual/en/features.file-upload.php

Upvotes: 2

Andreas
Andreas

Reputation: 5335

The variable $author will be automatically available if REGISTER GLOBALS is turned ON (which is not recommended). $_REQUEST is a superglobal holding POST and GET information.

I recommend you read about $_POST, $_GET and $_FILES

Upvotes: 4

Related Questions