dongluyi5123 2013-09-03 16:10
浏览 93
已采纳

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

I have a web server that receives a posted data form, one part of which can be an attached image. I want to use curl to send that information along to a second server. Everything works fine for requests that do not include an image, but when an image is included in the original post I get:

curl error 26: failed creating formpost data

I know this indicates that curl is unable to find the file, but I am having trouble determining exactly why not. Here is the code

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) . ")
";
    }
  }

  $testMsg .= "POSTDATA:
" . print_r($postData, true);

  $options[CURLOPT_POST] = true;        
  $options[CURLOPT_POSTFIELDS] = $postData;  
}

And here is the output:

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
)

The file indeed exists - why is curl giving me the error? I appreciate any help you can give...

Edit: I do not have this same problem when servers 1 and 2 are the same server (i.e. when I set up a web page on the second server that receives the posted data and then executes the same curl code to send the info along to itself). That probably means something, but I am evidently overlooking something...

展开全部

  • 写回答

1条回答 默认 最新

  • 普通网友 2013-09-04 06:39
    关注

    I'm not sure but perhaps you php or curl version doesn't support setting the content type this way. Try to remove the type= part from your code and let curl decide the content type:

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

    You could also try setting the CURLOPT_VERBOSE flag to true and see if you get a more descriptive error message.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
  • ¥15 IEC61850 客户端和服务端的通讯机制
  • ¥15 MAX98357a(关键词-播放音频)
  • ¥15 Linux误删文件,请求帮助
  • ¥15 IBMP550小型机使用串口登录操作系统
  • ¥15 关于#python#的问题:现已知七自由度机器人的DH参数,利用DH参数求解机器人的逆运动学解目前使用的PSO算法
  • ¥15 发那科机器人与设备通讯配置
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部