Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello all


i am new in c++
and working hard to upload an image to a php web Server.

but its not working below is my code

kindly have a look and help me...



C++
  #include <windows.h>
  #include <wininet.h>
  #include <iostream>
  #include <tchar.h>

#pragma comment(lib,"wininet.lib")
  #define ERROR_OPEN_FILE       10
  #define ERROR_MEMORY          11
  #define ERROR_SIZE            12
  #define ERROR_INTERNET_OPEN   13
  #define ERROR_INTERNET_CONN   14
  #define ERROR_INTERNET_REQ    15
  #define ERROR_INTERNET_SEND   16

  using namespace std;

  int main()
  {
     // Local variables
	 static char *filename   = "file";   //Filename to be loaded
	 static char *filepath   = "d:\\a.jpg";   //Filename to be loaded
     static char *type       = "image/jpeg";
     static char boundary[]  = "--BOUNDARY---";            //Header boundary
     static char nameForm[]  = "formname";     //Input form name
     static char iaddr[]     = "localhost";        //IP address
     static char url[]       = "/http/file.php";         //URL
	 
	 char hdrs[512]={'-'};                  //Headers
     char * buffer;                   //Buffer containing file + headers
     char * content;                  //Buffer containing file
     FILE * pFile;                    //File pointer
     long lSize;                      //File size
     size_t result;                   


     // Open file
     pFile = fopen ( filepath , "rb" );
     if (pFile==NULL) 
	 {
		 printf("ERROR_OPEN_FILE");
		 getchar();
		 return ERROR_OPEN_FILE;
	 }
	 printf("OPEN_FILE\n");

     // obtain file size:
     fseek (pFile , 0 , SEEK_END);
     lSize = ftell (pFile);
     rewind (pFile);
	
     // allocate memory to contain the whole file:
     content = (char*) malloc (sizeof(char)*lSize);
     if (content == NULL) 
	 {
		 printf("ERROR_MEMORY");
		 getchar();
		 return ERROR_OPEN_FILE;
	 }
     printf("MEMORY_ALLOCATED\t \"%d\" \n",&lSize);
     // copy the file into the buffer:
     result = fread (content,1,lSize,pFile);
     if (result != lSize) 
	 {
		 printf("ERROR_SIZE");
		 getchar();
		 return ERROR_OPEN_FILE;
	 }
     printf("SIZE_OK\n");

     // terminate
     fclose (pFile);
     printf("FILE_CLOSE\n");
     //allocate memory to contain the whole file + HEADER
     buffer = (char*) malloc (sizeof(char)*lSize + 2048);

     //print header
     sprintf(hdrs,"Content-Type: multipart/form-data; boundary=%s",boundary);
     sprintf(buffer,"%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n",boundary,nameForm,filename);
     sprintf(buffer,"%sContent-Type: %s\r\n",buffer,type);
	 sprintf(buffer,"%s%s",buffer,content);
     sprintf(buffer,"%s--%s--\r\n",buffer,boundary);



     //Open internet connection
     HINTERNET hSession = InternetOpen("WINDOWS",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
     if(hSession==NULL) 
	 {
		 printf("ERROR_INTERNET_OPEN");
		 getchar();
		 return ERROR_OPEN_FILE;
	 }
     printf("INTERNET_OPENED\n");

     HINTERNET hConnect = InternetConnect(hSession, iaddr,INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
     if(hConnect==NULL) 
	 {
		 printf("ERROR_INTERNET_CONN");
		 getchar();
		 return ERROR_INTERNET_CONN;
	 }
	 printf("INTERNET_CONNECTED\n");

     HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T(url),NULL, NULL, NULL,INTERNET_FLAG_RELOAD, 1);
     if(hRequest==NULL) 
	  {
		 printf("ERROR_INTERNET_REQ");
		 getchar();
		
	 }
	 printf("INTERNET_REQ_OPEN\n");

     BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));
    	
     if(!sent) 
	 {
		 printf("ERROR_INTERNET_SEND");
		 getchar();
		 return ERROR_INTERNET_CONN;
	 }
	 printf("INTERNET_SEND_OK\n");

     //close any valid internet-handles
     InternetCloseHandle(hSession);
     InternetCloseHandle(hConnect);
     InternetCloseHandle(hRequest);
    
	 
     
	 getchar();
	 return 0;
  }
Posted
Updated 4-Sep-13 18:06pm
v3
Comments
[no name] 3-Sep-13 5:11am    
you want to upload image using php..am i rite..??
Angel-cpp 3-Sep-13 5:50am    
i have c++ client that post image to php file@ server
but there is problem in c++ code..
Stefan_Lang 3-Sep-13 5:33am    
"not working" is not going to get you an answer anytime soon. Please be more concise: what is not working? What results do you see, and what is the result you'd expect? Just going through that code to understand your intentions can easily take an hour - don't expect us to take that much time just to understand the question - especially when it takes you less than a minute to provide that information!
Angel-cpp 3-Sep-13 5:52am    
hello stefan_lang

thanks to reply

my problem is when ever i execute this c++ code
its execute fine without a single error
but when i check my localhost php server there is no uploaded file

if you are able to correct this code then help me..
pasztorpisti 3-Sep-13 6:00am    
Read this to get some useful info: http://stackoverflow.com/questions/8659808/how-does-http-file-upload-work. You should send data similar to other clients (like a browser). Use wireshark/"follow tcp stream" to compare the data sent by a browser and your c++ code.

1 solution

Just skimming over your source (as you didn't describe what went wrong) the following lines stick out as a simple error:
C++
sprintf(buffer,"%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n",boundary,nameForm,filename);
 sprintf(buffer,"%sContent-Type: %s\r\n",buffer,type);
 sprintf(buffer,"%s%s",buffer,content);
 sprintf(buffer,"%s--%s--\r\n",buffer,boundary);

The second and third sprintf do not append to what you already have written, but overwrite it! Either use a single sprintf to do it all, or keep a pointer to where the next sprintf should go:
C++
char* p = buffer;
p += sprintf (p, "....", ....);
p += sprintf (p, "....", ....);

Of course, the above code is just for clarity. In a real application you would need to do some error checking as sprintf may return -1 in case of an error.
 
Share this answer
 
Comments
Stefan_Lang 3-Sep-13 6:09am    
Actually, the second and third sprintf do copy the previous state of the buffer to the front of the next version: the format string starts with %s and the buffer itself is passed as the first argument.
nv3 3-Sep-13 6:54am    
Right you are, Stefan! -- I didn't look that close. But now, I'd say that I would not bet on it that this kind of overlapping copy does work. Besides, this is all but efficient.

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