How to transfer a file using wininet, which is read using a PHP script?

I would like to send a text file to a web server using wininet, as if the file was submitted using a web form that sends the file to the server.

Based on the answers I received, I tried the following code:

static TCHAR hdrs[] = "Content-Type: multipart/form-data\nContent-Length: 25"; static TCHAR frmdata[] = "file=filename.txt\ncontent"; HINTERNET hSession = InternetOpen("MyAgent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); HINTERNET hConnect = InternetConnect(hSession, "example.com", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1); HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", "test.php", NULL, NULL, NULL, 0, 1); HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));"); 

The test.php script is executed, but it does not seem to receive the correct data.

Can someone give me extra help or take a look somewhere? Thanks.

+4
source share
3 answers

Changing the form data and the headers I had above solved the problem:

  static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"file.txt\"\nContent-Type: text/plain\n\nfile contents here\n-----------------------------7d82751e2bc0858--"; static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"; 
+1
source

Let me take this step at a time.

First, the HTTP Involved headers:

  • Content-Type: multipart / form-data li>
  • Content-Length: <It depends on the amount of content bytes>

Then you need to create a line with the contents of the POST form. Suppose you have an input file with the name:

file = filename.txt
<Now you add the contents of the file after carriage return>

You calculate the length of this string and put it in the Content-Length above.

Ok, the full HTTP request will look like this:

 POST /file_upload.php HTTP/1.0 Content-type: multipart/form-data Content-length: <calculated string length: integer> file=filename.txt ...File Content... 

Now some code from the PHP manual:

 <?php // In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead // of $_FILES. $uploaddir = '/var/www/uploads/'; $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>"; ?> 

Knowing me, I probably messed up the format for the content, but that's a general idea.

+2
source

Here's a general description of things related to this. Basically, you need to create an HTTP request to the web address, attach the information to the request, and then send it. The request should be a POST request in your case.

0
source

Source: https://habr.com/ru/post/1277412/


All Articles