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

时间:2013-09-04 00:10:10

标签: php file-upload curl

我有一个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是同一台服务器时,我没有同样的问题(即当我在第二台服务器上设置一个接收发布数据的网页,然后执行相同的卷曲代码来发送信息时一直到自己)。这可能意味着什么,但我显然忽视了一些事情......

1 个答案:

答案 0 :(得分:1)

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

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

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

相关问题