5

Objective:

I want to send multiple images using multipart/form-data. Below is a snippet of my code.

Problem:

On one PC, the multipart attachment is sent by the correct MIME header Content-Type: image/jpeg, but on another PC, the MIME header is Content-Type: application/octet.

Question:

How can I force cURL to set the correct Content-Type header for the MIME content?

$ch = curl_init();

$params = array('name' => '@D:\globe.jpg');  

$base_url = "https://example.com"."?".varEncode($test_data);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_URL,$base_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$result = curl_exec($ch);

1 Answer 1

18

Use the CURLOPT_HTTPHEADER option:

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: image/jpeg"));

So specify Content-Type headers specifically for file uploads, use:

$params = array('name'=>'@D:\globe.jpg;type=image/jpeg');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
Sign up to request clarification or add additional context in comments.

2 Comments

But this will change the Content-Type on HTTP header, not the MIME multipart header. PHP cURL seems to use magic.mime to detect the file type and auto-magically add the Content-Type header. Unfortunately, the magic.mime cannot detect a lot of file types and simple use application/octet-stream.
You can also set a custom file name:@D:\globe.jpg;type=image/jpeg;filename=my_picture.jpg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.