Ahmet Can Güven
Ahmet Can Güven

Reputation: 5462

cURL file upload problem

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.

Upvotes: 0

Views: 1349

Answers (1)

bisko
bisko

Reputation: 4078

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);

Upvotes: 1

Related Questions