0

I am trying to upload a picture using php cURL. But something is wrong about it.

This is the post data

-----------------------------192153227918513
Content-Disposition: form-data; name="resimbaslik"

CCClient
-----------------------------192153227918513
Content-Disposition: form-data; name="imgfile"; filename="clinteastwoodo.jpg"
Content-Type: image/jpeg

And I am trying to upload my picture with this php code

    $postfields = array();
$postfields ["resimbaslik"] = "CCClient";
$postfields ["imgfile"] = "filename=\"clinteastwoodo.jpg\"";
$referer = "http://www.example.com/ex1.php";
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/example.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_REFERER, $referer);
$result = curl_exec ($ch);

And it is giving me 417 - Expectation Failed error. The picture is in the same directory with my .php file.

Can someone help me to fix it?

Thank you.

1 Answer 1

1

Is the server you are trying to POST to a Lighttpd ? There is a known bug for Lighty in handling the Expect header, which creates just this situation. More information can be found here: http://redmine.lighttpd.net/issues/1017 .

In the comments from the above link, an easy fix is pointed out for PHP and cURL:

<?php
$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Expect:"));
//Other setopt, execute etc...
?>

You need to set an empty value for the Expect header. In your code above you just have to add the curl_setopt line. Something like this:

$postfields = array();
$postfields ["resimbaslik"] = "CCClient";
$postfields ["imgfile"] = "filename=\"clinteastwoodo.jpg\"";
$referer = "http://www.example.com/ex1.php";
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/example.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Expect:")); // << add this line.
$result = curl_exec ($ch);
Sign up to request clarification or add additional context in comments.

2 Comments

No not Lighttp. It couldnt solve my problem. I tried this before. And how can I use header for this post ?
$postfields ["imgfile"] = "filename=\"clinteastwoodo.jpg\""; A field without @ for a file upload? Also, $postfield['imgfile'] will be the fieldname on the form, so "filename= has no sense.

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.