How do you generate Boundary In PHP?
-
24-06-2021 - |
Question
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
Solution
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);
OTHER TIPS
To answer the actual question, you can do something like this:
$boundary = hash('sha256', uniqid('', true));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow