473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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|Wrap|Line Numbers
Expand|Select|Wrap|Line Numbers
  1.    1. hFileXfer = InternetOpen("FileSession", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  2.    2. if(hFileXfer != 0)
  3.    3.     hConnect = InternetConnect(hFileXfer, "localhost", 80, "", "", INTERNET_SERVICE_HTTP, 0,0);
  4.    4.     if(hConnect != 0)
  5.    5.     {
  6.    6.         DWORD flags;
  7.    7.         flags = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_PRAGMA_NOCACHE;
  8.    8.         hReq = HttpOpenRequest(hConnect,"POST", "./TargetDir/ClientUpload.php", NULL, NULL, NULL, flags, 0);
  9.    9.         if(hReq != 0)
  10.   10.         {
  11.   11.             if(HttpSendRequestEx(hReq, NULL, NULL,0, NULL))
  12.   12.             {
  13.   13.                 do
  14.   14.                 {
  15.   15.                     if(bRead = (bool)ReadFile(hSrcFile,&pBuffer, sizeof(pBuffer), &dwBytesRead, NULL))
  16.   16.                     {
  17.   17.                         if(!(bRtn =(bool)InternetWriteFile(hReq, pBuffer, dwBytesRead, &dwBytesWritten)))
  18.   18.                         {
  19.   19.                             DWORD DwError = GetLastError();
  20.   20.                             *ErrStr = "Unable to write file to server";
  21.   21.                             break;
  22.   22.                         }
  23.   23.                         dwTotalWritten += dwBytesWritten;
  24.   24.                     }
  25.   25.                     else
  26.   26.                     {
  27.   27.                         DWORD DwError = GetLastError();
  28.   28.                         *ErrStr = "Error reading source file";
  29.   29.                         bRtn = false;
  30.   30.                         break;
  31.   31.                     }
  32.   32.                 } while(dwTotalWritten < dwFileSize);
  33.   33.                 CloseHandle(hSrcFile);
  34.   34.                 TCHAR            RtnBuffer[1000];
  35.   35.                 DWORD            ret;
  36.   36.                 bRtn = InternetReadFile(hReq, RtnBuffer, 1000, &ret);
  37.   37.                 InternetCloseHandle(hReq);
  38.   38.                 InternetCloseHandle(hConnect);
  39.   39.                 InternetCloseHandle(hFileXfer);
  40.   40.             }
  41.   41.         }
  42.   42.     }
  43.   43. }
  44.  
PHP code
Expand|Select|Wrap|Line Numbers
Expand|Select|Wrap|Line Numbers
  1.    1. <?php
  2.    2. $uploaddir = '/TargetDir/ClientUploads/';
  3.    3. $uploadfile = $uploaddir . basename($_FILES['file']['name']);
  4.    4.  
  5.    5. echo '<pre>';
  6.    6. if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
  7.    7.     echo "File is valid, and was successfully uploaded.\n";
  8.    8. } else {
  9.    9.     echo "Possible file upload attack!\n";
  10.   10. }
  11.   11.  
  12.   12. echo 'Here is some more debugging info:';
  13.   13. print_r($_FILES);
  14.   14.  
  15.   15. print "</pre>";
  16.   16.  
  17.   17. ?>
  18.  
Apr 21 '09 #1
3 9902
Banfa
9,065 Expert Mod 8TB
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.
Apr 22 '09 #2
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:
Expand|Select|Wrap|Line Numbers
  1. csPost.Format("%s\r\n%s\r\n%s%d\r\n%s\r\n\r\n", 
  2.           "POST /ClientUpload.php HTTP/1.0",
  3.           "Content-type:application/x-www-form-urlencoded",
  4.           "Content-length:",dwFileSize,
  5.           "-- boundary");
  6. hReq = HttpOpenRequest(hConnect,csPost, NULL, NULL, NULL, NULL, flags, 0);
  7.  
The OpenRequest:csPost contains:
POST /ClientUpload.php HTTP/1.0
Content-type:application/x-www-urlencoded
Content-length:26
--boundary
GetLastError returns 87(Message: The parameter is incorrect in function)
returned handle is NULL
Apr 22 '09 #3
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 HttpSendRequestEx() with NULL or 0
Passed the header in with a internetWriteFile
followed by the file.
THANX to all for helping
Apr 22 '09 #4

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

Similar topics

3
by: WM Chung | last post by:
Hello all, My dotnet application configuration is that the client, Web server and database all reside in 3 different computers. There is a function which can browse a file and read in the...
1
by: alfredfx | last post by:
i'm planning to develop a client server system the client and server both able to upload picture client will upload the picture to a folder that reside in the server while server user can also...
6
by: jake | last post by:
How do you get or save a file from/to a client (button on web page) once they are authenticated? The client could be Mac, Linux or Windows. Can I do it all with IIS and ASP.NET?
3
by: Rob Nicholson | last post by:
The following code added to Page_Load works a treat: Response.ContentType = "application/zip" Response.AppendHeader("Content-Disposition", "attachment; filename=DesktopChange.pdf")...
9
by: CGW | last post by:
I asked the question yesterday, but know better how to ask it, today: I'm trying to use the File.Copy method to copy a file from a client to server (.Net web app under IIS ). It looks to me that...
6
by: dinoo | last post by:
Hi, I would appreciate if some one could help me out. I have to read a client side ini file in Aspx page and use that data in server side processing. Can any one help me out here? Please refer...
10
by: Paul | last post by:
Hi I am using the HtmlInputFile control to upload a file from a client to a server. I have a browse to find the file on the server but need to create the path dynamically as to were it will go...
12
by: SAL | last post by:
Hello, Is it possible to read a CSV from the Client, and bind my Datagrid to the data in the CSV file without uploading the file to the Server first? I have tried and in Debug mode on my...
3
by: pbd22 | last post by:
Hi There. I think i need some software design help. I have a page, server.aspx, that starts uploading video files when it is loaded via hidden iframe by the client. On the same page is an...
1
by: SGilch | last post by:
I am new to javascript, and wondering if it is capable of implementing a browsing mechanism for the browser to upload the names of files in a client side directory to the server. The client would...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.