Click here to Skip to main content
15,867,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

What I have tried:

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
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();
Posted
Updated 3-Apr-23 12:52pm
Comments
Member 12899279 25-Feb-20 2:48am    
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
Member 14030444 17-Jan-21 7:59am    
thanks, it really works.
Member 13187347 3-Sep-21 10:13am    
Can you please share the PHP code?
Member 15969676 3-Apr-23 18:55pm    
I tried above code but I am getting the 400 Bad Request (text/plain). I am also using c++ and curl_formadd as mentioned above. Any help will be highly appreciated.

1 solution

See the comments at libcurl - curl_easy_perform()[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900