dougou3871 2015-08-27 12:03
浏览 100
已采纳

LibCURL将文件从C ++上传到PHP - 需要411长度

I'm attempting to write a file to a server from my local machine using LibCURL, C++, and php, and I am running into an error, instead of the form completing, I am getting the error message "411 length required".

I am working from this example

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

This is my php form which I have verified as working

<!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>

And this is the libcurl code which is pretty faithful to the example apart from the 3 lines in the set upload file section. This is something which I tried based on a different example.

//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;

I have tried adding a header with the string "Content-Length: 0" and "Content-Length: 44000" (the size of the test jpg) but neither change the error.

there are similar questions on here but only one is using c++ which is this one

error 411 Length Required c++, libcurl PUT request

but it doesn't quite fit my situation and doesn't provide any answers either.

  • 写回答

1条回答 默认 最新

  • dongyou6795 2015-08-27 12:09
    关注

    Set CURLOPT_INFILESIZE like so: curl_easy_setopt(handle, CURLOPT_INFILESIZE, 44000);

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

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn