cURL文件上传问题

时间:2011-09-12 09:21:24

标签: php html curl

我正在尝试使用php cURL上传图片。但是它有些不对劲。

这是发布数据

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

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

我正在尝试使用此php代码上传我的图片

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

它给了我417 - Expectation Failed错误。 该图片与我的.php文件位于同一目录中。

有人可以帮我解决吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

您尝试将服务器发布到Lighttpd? Lighty在处理 Expect 标题时存在一个已知错误,这会产生这种情况。可以在此处找到更多信息:http://redmine.lighttpd.net/issues/1017

在上面链接的评论中,指出了PHP和cURL的简单修复:

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

您需要为Expect标头设置一个空值。在上面的代码中,您只需添加 curl_setopt 行。像这样:

$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);
相关问题