2015-08-27 36 views
0

我試圖從我的本地機器上使用LibCURL,C++和php編寫文件到服務器,並且遇到錯誤而不是表單完成,我收到錯誤消息「411需要長度」。LibCURL將文件從C++上傳到PHP - 411需要的長度

我從這個例子中工作

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

這是我已經驗證作爲工作

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

我的PHP形式,並且這是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」 和 「內容長度:44000」(測試JPG的大小)但既不變化的誤差。

這裏對於類似的問題,但只有一個是用C++這是這個

error 411 Length Required c++, libcurl PUT request

,但它並不完全適合我的情況,並沒有任何提供任何答案。

+1

代碼首先設置* UPLOAD(這意味着PUT),然後進一步設置* HTTPPOST(這意味着多部分形式POST)。你需要下定決心,你不能在同一個請求中同時使用! –

+0

您的代碼幾乎完全* dis *與您鏈接的示例代碼相似。它看起來像你一起粘貼兩個樣本的位,並希望最好。 – molbdnilo

+0

謝謝@DanielStenberg現在正在工作。 –

回答

1

設置CURLOPT_INFILESIZE像這樣: curl_easy_setopt(handle,CURLOPT_INFILESIZE,44000);

+0

謝謝,這擺脫了411錯誤,現在它說沒有指定文件,所以在PHP方面,$ _FILES ['sendfile'] ['name']等於「」,你能明白這是爲什麼嗎?當我調用curl_formadd時,我得到CURL_FORMADD_OK,並且我嘗試了CURLFORM_COPYCONTENTS –

相關問題