C++ client upload to PHP server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wwhitman
    New Member
    • Apr 2009
    • 7

    #1

    C++ client upload to PHP server

    'm having issues transferring a file from a WEB client running a C++ application to a PHP server script. I have debugged through the C++ side and no errors are reported, but the file does not appear on the server. My current environment is a Vista business system running IIS. I developed the C++ app using Microsoft's Visual Studio. I know the configuration is correct as the application does communicate with PHP scripts to transfer XML data back and forth. I cannot seem to get the file upload to work. I have attached both the C++ and PHP sides. Any assistance would be greatly appreciated.
    C++ code
    Expand|Select|W rap|Line Numbers
    Code:
       1. hFileXfer = InternetOpen("FileSession", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
       2. if(hFileXfer != 0)
       3.     hConnect = InternetConnect(hFileXfer, "localhost", 80, "", "", INTERNET_SERVICE_HTTP, 0,0);
       4.     if(hConnect != 0)
       5.     {
       6.         DWORD flags;
       7.         flags = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE;
       8.         hReq = HttpOpenRequest(hConnect,"POST", "./TargetDir/ClientUpload.php", NULL, NULL, NULL, flags, 0);
       9.         if(hReq != 0)
      10.         {
      11.             if(HttpSendRequestEx(hReq, NULL, NULL,0, NULL))
      12.             {
      13.                 do
      14.                 {
      15.                     if(bRead = (bool)ReadFile(hSrcFile,&pBuffer, sizeof(pBuffer), &dwBytesRead, NULL))
      16.                     {
      17.                         if(!(bRtn =(bool)InternetWriteFile(hReq, pBuffer, dwBytesRead, &dwBytesWritten)))
      18.                         {
      19.                             DWORD DwError = GetLastError();
      20.                             *ErrStr = "Unable to write file to server";
      21.                             break;
      22.                         }
      23.                         dwTotalWritten += dwBytesWritten;
      24.                     }
      25.                     else
      26.                     {
      27.                         DWORD DwError = GetLastError();
      28.                         *ErrStr = "Error reading source file";
      29.                         bRtn = false;
      30.                         break;
      31.                     }
      32.                 } while(dwTotalWritten < dwFileSize);
      33.                 CloseHandle(hSrcFile);
      34.                 TCHAR            RtnBuffer[1000];
      35.                 DWORD            ret;
      36.                 bRtn = InternetReadFile(hReq, RtnBuffer, 1000, &ret);
      37.                 InternetCloseHandle(hReq);
      38.                 InternetCloseHandle(hConnect);
      39.                 InternetCloseHandle(hFileXfer);
      40.             }
      41.         }
      42.     }
      43. }
    PHP code
    Expand|Select|W rap|Line Numbers
    Code:
       1. <?php
       2. $uploaddir = '/TargetDir/ClientUploads/';
       3. $uploadfile = $uploaddir . basename($_FILES['file']['name']);
       4.  
       5. echo '<pre>';
       6. if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
       7.     echo "File is valid, and was successfully uploaded.\n";
       8. } else {
       9.     echo "Possible file upload attack!\n";
      10. }
      11.  
      12. echo 'Here is some more debugging info:';
      13. print_r($_FILES);
      14.  
      15. print "</pre>";
      16.  
      17. ?>
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    What headers are you sending with your HTTP request? Because I believe at a minimum you will need to send "Content-Type: application/x-www-form-urlencoded" but I see no mention of it.

    Comment

    • wwhitman
      New Member
      • Apr 2009
      • 7

      #3
      What you see is what I am sending or better 'trying to send'. I was not aware that I had to send a header.
      I have tried this as well -except the content-type was different. I replaced the HttpOpenRequest call with the following:
      Code:
      csPost.Format("%s\r\n%s\r\n%s%d\r\n%s\r\n\r\n", 
      		  "POST /ClientUpload.php HTTP/1.0",
      		  "Content-type:application/x-www-form-urlencoded",
      		  "Content-length:",dwFileSize,
      		  "-- boundary");
      hReq = HttpOpenRequest(hConnect,csPost, NULL, NULL, NULL, NULL, flags, 0);
      The OpenRequest:csP ost contains:
      POST /ClientUpload.ph p HTTP/1.0
      Content-type:applicatio n/x-www-urlencoded
      Content-length:26
      --boundary
      GetLastError returns 87(Message: The parameter is incorrect in function)
      returned handle is NULL

      Comment

      • wwhitman
        New Member
        • Apr 2009
        • 7

        #4
        I got the C++ side to execute without errors:
        This is what I did:
        on the HttpOpenRequest I passed "POST" and the server.php file name
        then ran HttpSendRequest Ex() with NULL or 0
        Passed the header in with a internetWriteFi le
        followed by the file.
        THANX to all for helping

        Comment

        Working...