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

  • Question

  • 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

    Tuesday, February 25, 2020 5:38 AM

Answers

  • 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

    i was able to accomplish this with the following code

    CURL *curl;
    CURLcode res;

    curl_httppost* post = NULL;
    curl_httppost* last = NULL;
    /*HttpPost* post = NULL;
    HttpPost* last = NULL;*/

    curl = curl_easy_init();
    if (curl)
    {
    curl_formadd(&post, &last,
    CURLFORM_COPYNAME, "name2",
    CURLFORM_COPYCONTENTS, "CV",
    CURLFORM_END);
    curl_formadd(&post, &last,
    CURLFORM_COPYNAME, "file2",
    CURLFORM_FILE, "E:\\BIMS Uni\\CV.docx",
    CURLFORM_END);



    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8081/PCInfo/test.php");
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);

    res = curl_easy_perform(curl);
    if (res)
    {
    return 0;
    }

    curl_formfree(post);
    }
    else
    {
    return 0;
    }

    curl_easy_cleanup(curl);

    But now my issue is that i want to upload multiple files i have directory paths of these files and i want them to upload to the server automatically with some kind of loop and also in above method i am unable to extract filename and i am supplying the file name via hardcoding which i don't want to use

    • Marked as answer by Learner177 Wednesday, March 11, 2020 5:44 AM
    Tuesday, February 25, 2020 7:48 AM

All replies

  • 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

    i was able to accomplish this with the following code

    CURL *curl;
    CURLcode res;

    curl_httppost* post = NULL;
    curl_httppost* last = NULL;
    /*HttpPost* post = NULL;
    HttpPost* last = NULL;*/

    curl = curl_easy_init();
    if (curl)
    {
    curl_formadd(&post, &last,
    CURLFORM_COPYNAME, "name2",
    CURLFORM_COPYCONTENTS, "CV",
    CURLFORM_END);
    curl_formadd(&post, &last,
    CURLFORM_COPYNAME, "file2",
    CURLFORM_FILE, "E:\\BIMS Uni\\CV.docx",
    CURLFORM_END);



    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:8081/PCInfo/test.php");
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);

    res = curl_easy_perform(curl);
    if (res)
    {
    return 0;
    }

    curl_formfree(post);
    }
    else
    {
    return 0;
    }

    curl_easy_cleanup(curl);

    But now my issue is that i want to upload multiple files i have directory paths of these files and i want them to upload to the server automatically with some kind of loop and also in above method i am unable to extract filename and i am supplying the file name via hardcoding which i don't want to use

    • Marked as answer by Learner177 Wednesday, March 11, 2020 5:44 AM
    Tuesday, February 25, 2020 7:48 AM
  • Hi,

    Thank you for posting here.

    This forum is about c++ development. According to your issue , I suggest you should ask it in a Curl Global Community for better support.

    Best Regards,

    Jeanine Zhang
    Tuesday, February 25, 2020 7:53 AM
  • i posted it here because i am not just using curl but curl with c++ language any way thanks for the suggestion
    Wednesday, February 26, 2020 3:46 AM