C++ client upload to PHP server

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

    C++ client upload to PHP server

    I'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
    Code:
    hFileXfer = InternetOpen("FileSession", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if(hFileXfer != 0)
    	hConnect = InternetConnect(hFileXfer, "localhost", 80, "", "", INTERNET_SERVICE_HTTP, 0,0);
    	if(hConnect != 0)
    	{
    		DWORD flags;
    		flags = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE;
    		hReq = HttpOpenRequest(hConnect,"POST", "./TargetDir/ClientUpload.php", NULL, NULL, NULL, flags, 0);
    		if(hReq != 0)
    		{
    			if(HttpSendRequestEx(hReq, NULL, NULL,0, NULL))
    			{
    				do
    				{
    					if(bRead = (bool)ReadFile(hSrcFile,&pBuffer, sizeof(pBuffer), &dwBytesRead, NULL))
    					{
    						if(!(bRtn =(bool)InternetWriteFile(hReq, pBuffer, dwBytesRead, &dwBytesWritten)))
    						{
    							DWORD DwError = GetLastError();
    							*ErrStr = "Unable to write file to server";
    							break;
    						}
    						dwTotalWritten += dwBytesWritten;
    					}
    					else
    					{
    						DWORD DwError = GetLastError();
    						*ErrStr = "Error reading source file";
    						bRtn = false;
    						break;
    					}
    				} while(dwTotalWritten < dwFileSize);
    				CloseHandle(hSrcFile);
    				TCHAR			RtnBuffer[1000];
    				DWORD			ret;
    				bRtn = InternetReadFile(hReq, RtnBuffer, 1000, &ret);
    				InternetCloseHandle(hReq);
    				InternetCloseHandle(hConnect);
    				InternetCloseHandle(hFileXfer);
    			}
    		}
    	}
    }

    PHP code
    Code:
    <?php
    $uploaddir = '/TargetDir/ClientUploads/';
    $uploadfile = $uploaddir . basename($_FILES['file']['name']);
    
    echo '<pre>';
    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
        echo "File is valid, and was successfully uploaded.\n";
    } else {
        echo "Possible file upload attack!\n";
    }
    
    echo 'Here is some more debugging info:';
    print_r($_FILES);
    
    print "</pre>";
    
    ?>
    Last edited by Markus; Apr 21 '09, 02:31 PM. Reason: Added [code] tags.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    What output do you get? Any PHP errors?

    Comment

    • wwhitman
      New Member
      • Apr 2009
      • 7

      #3
      Appears I get nothing from PHP side. I am not even sure it gets activated. I think I remember putting in some code on the PHP to write to an MySQL Db and nothing appeared, and it was the first thing I did

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Unfortunately I am out of ideas for this :/
        I know squat about C++.

        Maybe someone else will be able to help.

        Comment

        • wwhitman
          New Member
          • Apr 2009
          • 7

          #5
          UPDATE: I think I got the C++ side working. I can go through the code and no errors are reported. The PHP script gets executed but there is no file there
          Code:
          $connection = mysql_connect('localhost','root',''); // or die('Error: ' . mysql_error()); 
          if(!$connection)
             $StatusStr = 'DatabaseError';
          else
          {
             mysql_select_db("watchdog", $connection);
             $sql = "INSERT INTO testTbl ( TestSeen) VALUES ( '1')";
             $Result = mysql_query($sql);
          }
          $uploaddir = 'ClientUploads/';
          if(empty($_FILES["uploaded_file"]))
          {
          	$sql = "INSERT INTO testTbl ( TestSeen) VALUES ( 'EMPTY')";
          	$Result = mysql_query($sql);
          }
          In the table I see 1 followed by EMPTY

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            I'm not sure you're able to send a file like that and have PHP recognise it. Does C++ have access to a cURL library?

            Comment

            • wwhitman
              New Member
              • Apr 2009
              • 7

              #7
              NOPE.
              This should work.
              I get no errors on the C++ side.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                If there is no data in the FILES array, then your C++ application is not submitting the data correctly.

                Comment

                Working...