无法使用libcurl将.txt文件从ubuntu linux上载到apache http服务器

无法使用libcurl将.txt文件从ubuntu linux上载到apache http服务器,c,http,curl,post,libcurl,C,Http,Curl,Post,Libcurl,我正在尝试使用libcurl将“.txt”文件从Ubuntu Linux上传到apache http服务器 我已经在Linux系统中编写了下面的c代码来执行http post上传到我的服务器 #include <stdio.h> #include <curl/curl.h> #include <sys/stat.h> int main(void) { CURL *curl; CURLcode res; FILE *fd; struct sta

我正在尝试使用libcurl将“.txt”文件从Ubuntu Linux上传到apache http服务器

我已经在Linux系统中编写了下面的c代码来执行http post上传到我的服务器

#include <stdio.h>
#include <curl/curl.h>
#include <sys/stat.h>

int main(void)
{
  CURL *curl;
  CURLcode res;
  FILE *fd;
  struct stat file_info;

  fd = fopen("/home/shantanu/test.txt", "rb"); /* open file to upload */ 
  if(!fd)
    return 1; /* can't continue */ 

  /* In windows, this will init the winsock stuff */ 
  /* to get the file size */ 
  if(fstat(fileno(fd), &file_info) != 0)
    return 1; /* can't continue */

   curl_global_init(CURL_GLOBAL_ALL);
   /* get a curl handle */ 
   curl = curl_easy_init();

   if(curl) {
    /* First set the URL that is about to receive our POST. This URL can
       just as well be a https:// URL if that is what should receive the
       data. */ 
    curl_easy_setopt(curl, CURLOPT_URL, "http://x.x.x.x/dashboard/");

    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE,(curl_off_t)file_info.st_size);

    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    /* Now specify the POST data */ 
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS,fd);

    /* Perform the request, res will get the return code */ 
    res = curl_easy_perform(curl);

    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }

  curl_global_cleanup();
  return 0;
}
#包括
#包括
#包括
内部主(空)
{
卷曲*卷曲;
卷曲编码;
文件*fd;
结构统计文件信息;
fd=fopen(“/home/shantanu/test.txt”,“rb”);/*打开文件上传*/
如果(!fd)
返回1;/*无法继续*/
/*在windows中,这将初始化winsock文件*/
/*要获取文件大小*/
如果(fstat(文件号(fd),&文件信息)!=0)
返回1;/*无法继续*/
curl\u global\u init(curl\u global\u ALL);
/*获取卷曲手柄*/
curl=curl_easy_init();
if(curl){
/*首先设置即将接收我们帖子的URL。此URL可以
如果这是应该接收的内容,那么最好是一个https://URL
数据。*/
curl\u easy\u setopt(curl,CURLOPT\u URL,“http://x.x.x.x/dashboard/");
curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,(curl_off_t)file_info.st_size);
卷曲度(卷曲度,卷曲度(1L));
/*现在指定POST数据*/
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,fd);
/*执行请求时,res将获得返回代码*/
res=旋度(curl)\u容易执行(curl);
/*检查错误*/
如果(res!=卷曲(OK)
fprintf(stderr,“curl\u easy\u perform()失败:%s\n”,
卷曲(容易的);
/*始终清理*/
旋度\轻松\清洁(旋度);
}
curl_global_cleanup();
返回0;
}
============================================= 在服务器端的dashboard文件夹中,我有一个名为index.php的文件,下面是将该文件移动到该位置的代码

<?PHP
 move_uploaded_file($_FILE['content']['tmp_name'], "test.txt");
?>

=================================================================

但是文件没有上传,我得到了以下o/p

*   Trying 1x.6x.32.157...
* Connected to 1x.6x.32.157 (1x.6x.32.157) port 80 (#0)
> POST /dashboard/ HTTP/1.1
Host: 1x.6x.32.157
Accept: */*
Content-Length: 2487
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue

< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Date: Fri, 11 May 2018 10:32:20 GMT
< Server: Apache/2.4.33 (Win32) OpenSSL/1.1.0g PHP/7.2.4
< X-Powered-By: PHP/7.2.4
< Content-Length: 117
< Content-Type: text/html; charset=UTF-8
< 
<br />
<b>Notice</b>:  Undefined index: content in <b>C:\xampp\htdocs\dashboard\index.php</b> on line <b>2</b><br />
*正在尝试1x.6x.32.157。。。
*连接到1x.6x.32.157(1x.6x.32.157)端口80(#0)
>POST/dashboard/HTTP/1.1
主机:1x.6x.32.157
接受:*/*
内容长度:2487
内容类型:application/x-www-form-urlencoded
预期:100人继续

注意:未定义索引:第2行C:\xampp\htdocs\dashboard\index.php中的内容

想要一个
char*
,您要传递一个
文件*
所以
curl\u easy\u setopt(curl,CURLOPT\u POSTFIELDS,(char*)fd)?谢谢Daniel和Hanshenrik,我将尝试上面的方法。不,
CURLOPT_POSTFIELDS
需要字符串,而不是文件描述符。如果你想读取一个文件,你需要
CUROPT\u READFUNCTION
@Shantanu no stop,不要这样做,我只是在开玩笑(这是一个愚蠢的玩笑,对不起),如果你可以将文件原始发送为post主体(在这种情况下,你必须将你的php代码更改为
$in=fopen)的话,请使用CURLOPT\u READDATA而不是CURLOPT\u POSTFIELDS("php://input“,”rb“;$dest=fopen(“test.txt”,“wb”);流复制到流($in,$dest);fclose($in);fclose($dest)
-或者如果出于某种原因需要使用$\u文件或使用多部分/表单数据,…可以使用libcurl c api中的
多部分/表单数据
,但我不确定如何使用。Stenberg可能会这样做,tho.Hi Daniel&Hanshenrik。谢谢,我使用的是“application/x-www-form-urlencoded”,在默认情况下,这是不正常的,因为我正在进行文件上载。但正如您所建议的,我尝试使用libcurl使用“multipart/form data”,现在它正在使用我之前询问的.php代码上载文件。