How do I upload files from C++ using curl to PHP server

  • Thread starter Thread starter Shahzaib17
  • Start date Start date
S

Shahzaib17

Guest
Please don't give links of curl documentation or examples as they are very old and confusing
i just need a very simple example in which if i send files of type .docx or .pdf from c++ to my php script i can save them in folder on php server

this is what i know how i can send values of variables from c++ using curl but i am looking for method to upload files to folders in php server and not in tables
Hide Copy Code

CURL * curl;
curl_global_init(CURL_GLOBAL_ALL);
CURLcode res;

string UserName = pcobj.getUserName();


string request = "UserName=" + UserName;
string url = "http://localhost:8084/project/Files.php";
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request.c_str());
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_global_cleanup();


and this is what i am using to try to send files from this location to my wamp server

CURL *curl;
CURLcode res;
struct stat file_info;
curl_off_t speed_upload, total_time;
FILE *fd;

fd = fopen("C:\\Uni\\CV.docx", "rb"); /* open file to upload */
if (!fd)
return 1; /* can't continue */

/* to get the file size */
if (fstat(fileno(fd), &file_info) != 0)
return 1; /* can't continue */

curl = curl_easy_init();
if (curl) {
/* upload to this place */
curl_easy_setopt(curl, CURLOPT_URL,
"http://localhost:8084/Server/new_folder_name");

/* tell it to "upload" to the URL */
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

/* set where to read from (on Windows you need to use READFUNCTION too) */
curl_easy_setopt(curl, CURLOPT_READDATA, fd);

/* and give the size of the upload (optional) */
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)file_info.st_size);

/* enable verbose for easier tracing */
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));

}
else {
/* now extract transfer info */
curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);
fprintf(stderr, "Speed: %" CURL_FORMAT_CURL_OFF_T " bytes/sec during %"
CURL_FORMAT_CURL_OFF_T ".%06ld seconds\n",
speed_upload,
(total_time / 1000000), (long)(total_time % 1000000));

}
/* always cleanup */
curl_easy_cleanup(curl);
}
fclose(fd);
return 0;
return status is okay but the file didn't appear in the new_folder_name folder

Continue reading...
 
Back
Top