2011-09-12 162 views
0

我想用php cURL上传图片。但有些事情是错误的。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

服务器是否正尝试发布到Lighttpd? Lighty在处理期望标题时存在一个已知的问题,这就造成了这种情况。更多信息可以在这里找到:http://redmine.lighttpd.net/issues/1017

在从上面的链接的评论,一个简单的办法是指出了PHP和卷曲:

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

您需要设置的期望头空值。在上面的代码中,您只需添加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); 
+0

No not Lighttp。它无法解决我的问题。我之前尝试过。我该如何使用这个帖子的标题? –

+0

$ postfields [“imgfile”] =“文件名= \”clinteastwoodo.jpg \“”; 没有@用于文件上传的字段?另外,$ postfield ['imgfile']将是表单中的字段名,所以“filename =没有意义。 – erm3nda