469,358 Members | 1,659 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,358 developers. It's quick & easy.

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
Expand|Select|Wrap|Line Numbers
  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|Wrap|Line Numbers
  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. ?>
Apr 21 '09 #1
7 3449
Markus
6,050 Expert 4TB
What output do you get? Any PHP errors?
Apr 21 '09 #2
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
Apr 21 '09 #3
Markus
6,050 Expert 4TB
Unfortunately I am out of ideas for this :/
I know squat about C++.

Maybe someone else will be able to help.
Apr 21 '09 #4
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
Expand|Select|Wrap|Line Numbers
  1. $connection = mysql_connect('localhost','root',''); // or die('Error: ' . mysql_error()); 
  2. if(!$connection)
  3.    $StatusStr = 'DatabaseError';
  4. else
  5. {
  6.    mysql_select_db("watchdog", $connection);
  7.    $sql = "INSERT INTO testTbl ( TestSeen) VALUES ( '1')";
  8.    $Result = mysql_query($sql);
  9. }
  10. $uploaddir = 'ClientUploads/';
  11. if(empty($_FILES["uploaded_file"]))
  12. {
  13.     $sql = "INSERT INTO testTbl ( TestSeen) VALUES ( 'EMPTY')";
  14.     $Result = mysql_query($sql);
  15. }
  16.  
In the table I see 1 followed by EMPTY
Apr 22 '09 #5
Markus
6,050 Expert 4TB
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?
Apr 22 '09 #6
NOPE.
This should work.
I get no errors on the C++ side.
Apr 22 '09 #7
Markus
6,050 Expert 4TB
If there is no data in the FILES array, then your C++ application is not submitting the data correctly.
Apr 22 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by WM Chung | last post: by
1 post views Thread by alfredfx | last post: by
3 posts views Thread by Rob Nicholson | last post: by
6 posts views Thread by dinoo | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.