Can't upload file via curl

Viewed 155

Trying to build image autoupload php tool with curl. With other uploads like text, had no problems. But when it comes to image uploading part - I am stuck. Looked through other similar problems here at SO, but couldn't find anything that would help.

With Firefox developer tool I have found that headers sent are:

Host: www.xxxxxx.com
User-Agent: Mozilla/5.0 
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://www.xxxxxx.com
Content-Type: multipart/form-data; boundary=---------------------------54682012365
Content-Length: 13645
Origin: https://www.xxxxxx.com
DNT: 1
Connection: keep-alive
Cookie: PHPSESSID=xxxxx;
TE: Trailers

And body request:

-----------------------------54682012365
Content-Disposition: form-data; name="file"; filename="test.jpg"
Content-Type: image/jpeg

--->THERE GOES SOMETHING I BELIEVE TO BE IMAGETOTEXT(random symbols with few readable parts)<---
-----------------------------54682012365
Content-Disposition: form-data; name="ImId"

0
-----------------------------54682012365--

Tried to reproduce request with CURL

Sending headers as they are above. Body request:

$img = file_get_contents('test.jpg');
$body_message = 
'---------------------------54682012365
Content-Disposition: form-data; name="file"; filename="test.jpg"
Content-Type: image/jpeg

'.$img.'
---------------------------54682012365
Content-Disposition: form-data; name="ImId"

0
---------------------------54682012365--';

Curl options:

CURLOPT_RETURNTRANSFER => true, 
CURLOPT_HEADER => 1, 
CURLINFO_HEADER_OUT => true, 
CURLOPT_POST => 1, 
CURLOPT_POSTFIELDS => $body_message

But always get custom JSON error 'No images selected';

Tried various modifications with $body_message, tried a little bit with Curlfile, but still no success. Any thoughts?

1 Answers

There was a silly mistake, but it took some time to realise. If anyone comes with mistake like mine: boundary in header and body request are different length

---------------------------54682012365
-----------------------------54682012365

And in my $body_message boundary was 2 dashes short

Related