我正在尝试使用 CURL 从 C++ 将图片(及其拇指)上传到我的服务器。
C++ 目前没有问题,但要点如下:
想象这是我的 PHP 代码:
<?php
$valid = $_GET['valid'];
echo "hola";
file_put_contents('queries.txt', "eh!\n");
?>
当我调用我的 file.php
时,它必须在同一文件夹中写入一个“queries.txt”(它会这样做)。
现在,当从 C++ 调用 if 时,代码如下:
string url = "http://myserver.com/file.php";
CURL* curl = curl_easy_init();
// set target url
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
// holders
struct curl_httppost* beginPostList = NULL;
struct curl_httppost* endPostList = NULL;
string concat1 = tempFolder + imageName;
string concat2 = tempFolder + image200Name;
curl_formadd(&beginPostList, &endPostList, CURLFORM_COPYNAME, "image",
CURLFORM_FILE, &concat1, CURLFORM_END);
curl_formadd(&beginPostList, &endPostList, CURLFORM_COPYNAME, "thumb",
CURLFORM_FILE, &concat2, CURLFORM_END);
// perform
curl_easy_setopt(curl, CURLOPT_POST, true);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, beginPostList);
CURLcode code = curl_easy_perform(curl);
我当前的错误是没有调用服务器(因为没有创建“queries.txt”),但是如果我删除这些行:
curl_formadd(&beginPostList, &endPostList, CURLFORM_COPYNAME, "image",
CURLFORM_FILE, &concat1, CURLFORM_END);
curl_formadd(&beginPostList, &endPostList, CURLFORM_COPYNAME, "thumb",
CURLFORM_FILE, &image200Name, CURLFORM_END);
PHP 脚本被完美调用。那么,为什么 CURL 在添加任何文件时不调用我的脚本?
提前谢谢你。
编辑:
1.- 我尝试通过设置内容类型来调用它:
curl_formadd(&beginPostList, &endPostList, CURLFORM_COPYNAME, "image",
CURLFORM_FILE, &concat1,
CURLFORM_CONTENTTYPE, "image/jpeg", CURLFORM_END);
但什么也没发生
可能的错误:
我试图不设置任何内容,但我得到 HTTP STATUS = 200,但如果我设置要上传的任何文件,我得到 HTTP STATUS = 0...为什么?
最佳答案
当它需要 c 字符串 时,您传递的是 c++ 字符串
尝试:
curl_formadd(&beginPostList, &endPostList, CURLFORM_COPYNAME, "image",
CURLFORM_FILE, concat1.c_str(), CURLFORM_END);
curl_formadd(&beginPostList, &endPostList, CURLFORM_COPYNAME, "thumb",
CURLFORM_FILE, concat2.c_str(), CURLFORM_END);
关于php - 尝试使用 CURL 从 C++ 上传文件到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31185259/