0

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.

Community
  • 1
  • 1
cool mr croc
  • 725
  • 1
  • 13
  • 33
  • 1
    The code first sets *UPLOAD (which implies PUT) and then further down it sets *HTTPPOST (which implies multipart form POST). You need to make up your mind, you can't use both in the same request! – Daniel Stenberg Aug 27 '15 at 12:19
  • Your code is almost completely *dis*similar to the sample code you linked to. It looks like you pasted bits of two samples together and hoped for the best. – molbdnilo Aug 27 '15 at 12:28
  • thank you @DanielStenberg it's working now. – cool mr croc Aug 27 '15 at 12:39

1 Answers1

1

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

barbar
  • 26
  • 1
  • Thanks, that got rid of the 411 error, now it's saying no file is specified, so on the php side, $_FILES['sendfile']['name'] is equal to "", can you see why that is? I get CURL_FORMADD_OK when I call curl_formadd and I have tried CURLFORM_COPYCONTENTS – cool mr croc Aug 27 '15 at 12:13