Показать сообщение отдельно
  #3  
Старый 05.06.2011, 20:08
Homez Homez вне форума
Прохожий
 
Регистрация: 10.03.2011
Сообщения: 20
Репутация: 10
По умолчанию

Вот приложение (код не мой, прошу не пинать):
Код:
#pragma comment(lib, "wininet.lib" )

#include <windows.h>
  #include <wininet.h>
  #include <iostream>


  #define ERROR_OPEN_FILE       10
  #define ERROR_MEMORY          11
  #define ERROR_SIZE            12
  #define ERROR_INTERNET_OPEN   13
  #define ERROR_INTERNET_CONN   14
  #define ERROR_INTERNET_REQ    15
  #define ERROR_INTERNET_SEND   16

  using namespace std;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  {
     // Local variables
     static char *filename   = "C:\\autoexec.bat";   //Filename to be loaded
     static char *type       = "text/plain";
     static char boundary[]  = "pippo";            //Header boundary
     static char nameForm[]  = "uploadedfile";     //Input form name
     static char iaddr[]     = "localhost";        //IP address
     static char url[]       = "upload.php";         //URL

     char hdrs[255];                  //Headers
     char * buffer;                   //Buffer containing file + headers
     char * content;                  //Buffer containing file
     FILE * pFile;                    //File pointer
     long lSize;                      //File size
     size_t result;                   


     // Open file
     pFile = fopen ( filename , "rb" );
     if (pFile==NULL) return ERROR_OPEN_FILE;

     // obtain file size:
     fseek (pFile , 0 , SEEK_END);
     lSize = ftell (pFile);
     rewind (pFile);

     // allocate memory to contain the whole file:
     content = (char*) malloc (sizeof(char)*lSize + 1);
     if (content == NULL) return ERROR_MEMORY;

	 content[lSize] = '\0';

     // copy the file into the buffer:
     result = fread (content,1,lSize,pFile);
     if (result != lSize) return ERROR_SIZE;

     // terminate
     fclose (pFile);

     //allocate memory to contain the whole file + HEADER
     buffer = (char*) malloc (sizeof(char)*lSize + 2048);

     //print header
     sprintf(hdrs,"Content-Type: multipart/form-data; boundary=%s",boundary);
     sprintf(buffer,"--%s\r\nContent-Disposition: form-data; name=\"%s\"; data=\"%s\"\r\n",boundary,"autoexec.bat","autoexec.bat");
     sprintf(buffer,"%sContent-Type: %s\r\n\r\n",buffer,type);
     sprintf(buffer,"%s%s\r\n",buffer,content);
     sprintf(buffer,"%s--%s--\r\n",buffer,boundary);

     //Open internet connection
     HINTERNET hSession = InternetOpen("WinSock",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
     if(hSession==NULL) return ERROR_INTERNET_OPEN;

     HINTERNET hConnect = InternetConnect(hSession, iaddr,INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
     if(hConnect==NULL) return ERROR_INTERNET_CONN;

     HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",url, NULL, NULL, (const char**)"*/*\0", 0, 1);
     if(hRequest==NULL) return ERROR_INTERNET_REQ;

     BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));
     if(!sent) return ERROR_INTERNET_SEND;

	 char buf[1000];
	 DWORD nRead;
	 InternetReadFile(hRequest, buf, 1000, &nRead);
	 buf[nRead] = '\0';

     //close any valid internet-handles
     InternetCloseHandle(hSession);
     InternetCloseHandle(hConnect);
     InternetCloseHandle(hRequest);

     return 0;
  }
Вот PHP:
Код:
<?php 
        $path='./logs/'; 
        $return=''; 

            if(move_uploaded_file($_FILES['data']['tmp_name'], $path.$_FILES['data']['name'])){ 
                $return= "md5sum: ".md5_file($path.$_FILES['data']['name']); 
            } 
            else{$return='FALSE';} 

        echo $return; 
    ?>
Вот форма:
Код:
<form action="http://127.0.0.1/upload.php" method="post" enctype="multipart/form-data" >
<input type="file" name="data" /><br />
<input type="submit" value="send" />
<input type="hidden" name="submiited" value="1" />
</form>
Ответить с цитированием