tags:

views:

131

answers:

2

I have a raw form-data that look like this:

------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="intro"

O
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="title"

T
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="apiKey"

98d32fdsa
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="method"

/media/add
------------V2ymHFg03ehbqgZCaKO6jy
Content-Disposition: form-data; name="upload_field"; filename="original_filename.png"
Content-Type: image/png


------------V2ymHFg03ehbqgZCaKO6jy--

(In place of second line of upload_field there are data of this file (invisible here). So my question is:

How to parse above data to have a table:

$result['intro'] 

and so on with data inside?

+1  A: 
$boundary = "------------V2ymHFg03ehbqgZCaKO6jy"; // take this from content-type
$rawfields = explode($boundary,$data);
array_pop($rawfields); // drop the last -- piece
foreach ( $rawfields as $id=>$block )
{
    list($mime,$content) = explode("\r\n\r\n",$block,2); // I think it's <cr><lf> by standards, maybe check!
    if ( preg_match('/name="([^"]*)"/i',$mime,$match) )
    {
        $result[$match[1]] = $content;
        // todo: do funky stuff with other fields
    } else {
        $result[] = $content; // just in case...
    }
}

presto.

edit: you should also trim off the newline from each content block, but rtrim will chop more than one newline, so you have to get a little more creative.

mvds
Best exploding the string by new lines then get the first line as boundary, most of the time you will not know the boundry ! - an ragards to cf/nl try standerdizing the new lines `str_replace(array("\r","\n"),"\r\n",$string);`
RobertPitt
this is flawed anyway, should `preg_split("/-+$boundary/",$data)` instead of exploding. replacing newlines doesn't seem wise since you would be altering content as well. But then again the whole setup of parsing this by hand is flawed.
mvds
A: 

The content-type of the overall document is multipart, with the parts being separated by the given boundary.

Each part, or message, within the multipart document is in a standard message format: header lines, followed by a blank line, followed by a sequence of bytes representing the content of that part. Both HTTP and SMTP work like this.

With header lines like Content-Disposition: form-data; name="title", you have to be careful with the name because it could be encoded (think about how to represent an arbitrary value, such as one containing a double quote mark or a newline).

Justice