使用curl将文件从一个服务器传递到另一个服务器


use curl to pass file from one server to another

我有一个web服务器,它接收发布的数据表单,其中一部分可以是附加的图像。我想使用curl将该信息发送到第二个服务器。对于不包括图像的请求,一切都很好,但当图像包含在原始帖子中时,我得到:

curl error 26: failed creating formpost data

我知道这表明curl找不到文件,但我很难确定为什么不找到。这是代码

if ($_POST) {
  $postData = array();    
  //copy in the post fields that were posted here    
  foreach ($_POST as $key => $value) {
    $postData[$key] = $value;  
  }
  //copy in the files that have been included
  foreach ($_FILES as $key => $value) {
    if ($value["tmp_name"]) { //if null, no file was uploaded              
      $fn = "existing_image.jpg";//(use existing for debugging only
                                 //in reality, would use the tmp image)           
      $postData[$key] = "@" . realpath($fn) . ";type=" . $value["type"];
      $testMsg .= "added file $key value of '" . $postData[$key] .
          "'(" . file_exists($fn) . ")'n";
    }
  }
  $testMsg .= "POSTDATA:'n" . print_r($postData, true);
  $options[CURLOPT_POST] = true;        
  $options[CURLOPT_POSTFIELDS] = $postData;  
}

这是输出:

added file photo value of '@/var/www/wordpress/somedirectory/php/existing_image.jpg;type=image/jpeg'(1)
POSTDATA:
Array
(
    [key1] => 4
    [key2] => blah blah some text
    [key3] => etc
    [keyimage] => @/var/www/wordpress/somedirectory/php/existing_image.jpg;type=image/jpeg
)

文件确实存在——为什么curl会给我错误?我很感激你能提供的任何帮助。。。

编辑:当服务器1和2是同一台服务器时(即,当我在第二台服务器上建立一个网页,接收发布的数据,然后执行相同的curl代码将信息发送给自己时),我不会遇到同样的问题。这可能意味着什么,但我显然忽略了什么。。。

我不确定,但也许phpcurl版本不支持以这种方式设置内容类型。尝试从代码中删除type=部分,并让curl决定内容类型:

$postData[$key] = "@" . realpath($fn);

您也可以尝试将CURLOPT_VERBOSE标志设置为true,看看是否会收到更具描述性的错误消息。

相关文章: