我正在尝试使用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代码,除了设置的上载文件部分中的三行内容外,该示例非常真实。这是我根据其他示例尝试过的。

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

但这并不完全符合我的情况,也无法提供任何答案。

最佳答案

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

关于php - LibCURL从C++到PHP的上传文件-需要411个长度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32248877/

10-11 22:21
查看更多