开发者

Multipart form data parsing in PHP

开发者 https://www.devze.com 2023-01-24 17:41 出处:网络
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:

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:

开发者_StackOverflow$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?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消