【问题标题】:LibCURL upload file from C++ to PHP - 411 length requiredLibCURL 将文件从 C++ 上传到 PHP - 需要 411 长度
【发布时间】:2015-08-27 12:03:18
【问题描述】:

我正在尝试使用 LibCURL、C++ 和 php 从我的本地计算机将文件写入服务器,但我遇到了错误,而不是表单完成,我收到错误消息“需要 411 长度”。

我正在使用这个示例

http://curl.haxx.se/libcurl/c/postit2.html

这是我的 php 表单,我已验证它可以正常工作

<!DOCTYPE HTML> 
<html>
<head>
</head>
<body> 


 <form method="post" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
 Enter file: <input type="file" name="sendfile" size="40">
 <input type="submit" value="send" name="submit">
 </form>

<?php

echo “request method: " . $_SERVER[REQUEST_METHOD];
if( "$_SERVER[REQUEST_METHOD]" == "POST" || "$_SERVER[REQUEST_METHOD]" == "PUT")
{
    echo "Success <br>";

    if( $_FILES['sendfile']['name'] != "" )
    {
        $target_dir = "test/";
        $target_file = $target_dir . basename($_FILES["sendfile"]["name"]);

        if (move_uploaded_file($_FILES["sendfile"]["tmp_name"], $target_file)) 
        {
            echo "The file ". basename( $_FILES["sendfile"]["name"]). " has been uploaded.";
        } 
        else 
        {
            echo "Sorry, there was an error uploading your file.";
        }
    }
    else
    {
        die("No file specified!");
    }

} 
?>

</body>
</html>

这是 libcurl 代码,除了设置上传文件部分中的 3 行之外,它非常忠实于示例。这是我根据不同的示例尝试过的。

//setup   

curl_global_init(CURL_GLOBAL_ALL);
handle = curl_easy_init();
post = NULL;
last = NULL;
header_list = NULL;
uploadFile = NULL;

curl_easy_setopt(handle, CURLOPT_NOPROGRESS, ofGetLogLevel() <= OF_LOG_VERBOSE ? 0 : 1);
curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(handle, CURLOPT_VERBOSE, ofGetLogLevel() <= OF_LOG_VERBOSE);
curl_easy_setopt(handle, CURLOPT_CAINFO, ofToDataPath("cacert.pem").c_str());
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, content_writer);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &content);
curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, header_writer);
curl_easy_setopt(handle, CURLOPT_WRITEHEADER, &header);

//seturl
curl_easy_setopt(handle, CURLOPT_URL, “link to hosted php file on server”);

//add form field
curl_formadd(&post, &last, CURLFORM_COPYNAME, "sendfile", CURLFORM_FILE, “filepath on my local system”, CURLFORM_END);

//set upload file
FILE* uploadFile = fopen(“filepath on my local system”, "rb");
curl_easy_setopt(handle, CURLOPT_UPLOAD, 1);
curl_easy_setopt(handle, CURLOPT_READDATA, uploadFile);


//perform
CURLcode ret = curl_easy_setopt(handle, CURLOPT_VERBOSE, ofGetLogLevel() <= OF_LOG_VERBOSE);

ret = curl_easy_setopt(handle, CURLOPT_NOPROGRESS, ofGetLogLevel() <= OF_LOG_VERBOSE ? 0 : 1);

ret = curl_easy_setopt(handle, CURLOPT_HTTPPOST, post);

ret = curl_easy_perform(handle);

curl_formfree(post);
post = NULL;
fclose(uploadFile);
uploadFile = NULL;

我尝试使用字符串“Content-Length: 0”和“Content-Length: 44000”(测试 jpg 的大小)添加标题,但都没有改变错误。

这里有类似的问题,但只有一个是使用 c++ 的,就是这个

error 411 Length Required c++, libcurl PUT request

但它不太适合我的情况,也没有提供任何答案。

【问题讨论】:

  • 代码首先设置 *UPLOAD(这意味着 PUT),然后再进一步设置 *HTTPPOST(这意味着多部分表单 POST)。您需要下定决心,不能在同一个请求中同时使用两者!
  • 您的代码与您链接的示例代码几乎完全dis相似。看起来您将两个样本的一部分粘贴在一起并希望得到最好的结果。
  • 谢谢@DanielStenberg,它现在正在工作。

标签: php c++ libcurl


【解决方案1】:

像这样设置 CURLOPT_INFILESIZE: curl_easy_setopt(句柄, CURLOPT_INFILESIZE, 44000);

【讨论】:

  • 谢谢,摆脱了411错误,现在它说没有指定文件,所以在php方面,$_FILES['sendfile']['name']等于“”,你能明白这是为什么吗?当我调用 curl_formadd 时我得到 CURL_FORMADD_OK 并且我尝试过 CURLFORM_COPYCONTENTS
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-11
  • 1970-01-01
  • 2013-03-14
  • 2014-11-15
相关资源
最近更新 更多