c++ - libcURL buffered file upload not working -
i trying make file upload using libcurl. php errors if called script without post array.
code-listing 1: libcurl code
#include <iostream> #include <fstream> #include <cstring> #include <cstdlib> #include <curl/curl.h> int main(int argc, char *argv[]) { curl *curl; curlcode res; std::streampos size; char * memblock; std::ifstream file ("unnamed.png", std::ios::in|std::ios::binary|std::ios::ate); if (file.is_open()) { size = file.tellg(); memblock = new char [size]; file.seekg (0, std::ios::beg); file.read (memblock, size); file.close(); struct curl_httppost *formpost=null; struct curl_httppost *lastptr=null; curl_formadd(&formpost, &lastptr, curlform_copyname, "file", curlform_buffer, "unnamed.png", curlform_bufferptr, memblock, curlform_bufferlength, size, curlform_end); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, curlopt_url, "http://localhost/upload_file.php"); curl_easy_setopt(curl, curlopt_httppost, formpost); res = curl_easy_perform(curl); if(res != curle_ok) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); curl_formfree(formpost); } } return 0; } here php errors get:
( ! ) notice: undefined index: file in f:\wamp\www\upload_file.php on line 4 call stack # time memory function location 1 0.0003 240408 {main}( ) ..\upload_file.php:0 notice: undefined index: file in f:\wamp\www\upload.php on line 8 call stack # time memory function location 1 0.0003 242624 {main}( ) ..\upload.php:0 furthermore wonder if shouldn't add content type in way. know curlform_file has curlform_contenttype didn't see curlform_buffer.
edit: here html upload form , php file upload processing script.
code-listing 2: html form
<form enctype="multipart/form-data" action="upload.php" method="post"> send file: <input name="file" type="file" /> <input type="submit" value="send file" /> </form> code listing 3: php fileupload processing script
$uploaddir = '/'; $uploadfile = $uploaddir . basename($_files['file']['name']); echo "<p>"; if (move_uploaded_file($_files['file']['tmp_name'], $uploadfile)) { echo "file valid, , uploaded.\n"; echo '<pre>'; print_r($_files); print "</pre>"; } else { echo "upload failed"; } ?> the html code , php script (i used test) show when run on web browser message
file valid, , uploaded
followed print of array. not case when run libcurl code. php error messages , text
upload failed
it nasty bug, evident when found. problem indeed in call curl_formadd. when there incorrect parameter (which case, see below), function nothing, , return not null value. did not notice because forgot test return code (and did not expect error here ...)
so when post, empty post (no part) explains error in php script.
the problem parameter after curlform_bufferlength must long, , pass std::streampos.
the fix trivial (i add content type) :
long lsize = size.seekpos(); curlformcode code = curl_formadd(&formpost, &lastptr, curlform_copyname, "file", curlform_buffer, "unnamed.png", curlform_bufferptr, memblock, curlform_bufferlength, lsize, curlform_contenttype, "image/png", curlform_end); if (code != 0) { // avoid other problems ... } i control wireshark file bytes posted.
Comments
Post a Comment