gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:32:05 25 4
gpt4 key购买 nike

我正在尝试使用 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;
}

================================================在服务器端的仪表板文件夹中,我有一个名为 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 />

最佳答案

CURLOPT_POSTFIELDS想要一个 char *,你要向它传递一个 FILE *

关于c - 无法使用 libcurl 将 .txt 文件从 ubuntu linux 上传到 apache http 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50290856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com