Stacker Stacker - 2 months ago 10
PHP Question

How do you generate Boundary In PHP?

I'm using Curl library to simulate "multipart-form-data" submission, then I noticed that I need to generate a unique string, but I have no idea how to do that.

For example:

Content-Type: multipart/form-data;
boundary=---------------------------491299511942
Content-Length: 95418
-----------------------------491299511942
Content-Disposition: form-data; name="a"
-----------------------------491299511942
Content-Disposition: form-data; name="b"
/page.asp
-----------------------------491299511942
Content-Disposition: form-data; name="c"
-----------------------------491299511942
Content-Disposition: form-data; name="d"
true
-----------------------------491299511942
Content-Disposition: form-data; name="e"
1
-----------------------------491299511942
Content-Disposition: form-data; name="f"


How to generate that boundary? From library or just some hack?

EDIT: in this example Boundary=491299511942

Answer

if you using curl you don't need prepare data "manually"

$post_data = array(
    'a' => '@/absolute/path/to/file',
    'b' => '@/absolute/path/to/file',
);
curl_setopt($ch, CURLOPT_POST, true)
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
Comments