Forum Moderators: coopster

Message Too Old, No Replies

Why a separate global for $ FILES?

... instead of putting them in $_POST

         

idfer

6:51 pm on Sep 7, 2009 (gmt 0)

10+ Year Member



Hi all,

I'm writing some generic classes to process forms and right now, i'm implementing the file upload part, which is a headache because the values for uploaded files go to $_FILES instead of the usual $_POST. Why a separate global for these? It seems to me that the script would know which parameter is supposed to be a file and which one isn't, so instead of having, e.g.:

[pre]$_POST = ( 'caption' => 'blah' );
$_FILES = ( 'filename' => array (
'name' => 'example.jpg',
'type' => 'image/jpg',
...
);[/pre]

why not just have:

[pre]$_POST = ( 
'caption' => 'blah',
'filename' => array (
'name' => 'example.jpg',
'type' => 'image/jpg',
...
)
);[/pre]

In fact, i've started writing a function to merge all the values from $_FILES into $_POST at the top of the script. Would i run into any issues if i did that?

Thanks! Hope my question makes sense...

ETA: i suppose i need to be careful that there aren't separate POST variables called filename[name], filename[type], etc. or they'll get overwritten. Maybe stick all the upload parameters into a separate _file subarray.

rocknbil

4:18 pm on Sep 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Great Q, I would venture to guess that the files array is created only on multipart form/data. In perl, you can parse input from a regular form manually or with a short decoding routine. To parse a multipart form, with attachments (as, files are binary attachments to encoded submit data) we/I use the CGI library to read and parse.

This is because multipart form data has a "boundary" in the submitted data that defines the multiple parts. Regular form values (ASCII, text) go into one data set, attachments (binary) another. In a way, it's apples and oranges, and they are separated into $_POST and files by PHP (multipart fails with $_GET.)