/*
url = http://somesite.com
file = local file path
file2 = filename for upload
submit_name = the name of the submit html field on the page
submit_value = the value for the submit field
file_form_name = the file name field on the html page
mime_type = the http file type of the upload
folder_path = the path including file of the page to upload to ie: /pictures/user/index.php
*/
int UpLoad( const char *url, const char *file, const char *file2, const char *submit_name, const char *submit_value, 
const char *file_form_name, const char *mime_type, const char *folder_path )
{
    WSADATA wsaData;
    WSAStartup( MAKEWORD( 2,2 ), &wsaData );
    SOCKET sock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if( sock == INVALID_SOCKET )
    {
        return 0;
    }

    int value = 5000;
    if( setsockopt( sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&value, sizeof( int ) ) == SOCKET_ERROR )
    {
        closesocket( sock );
        return 0;
    }
    
    LPHOSTENT hostEntry;
    hostEntry = gethostbyname( url );

    if( hostEntry == NULL )
    {
        closesocket( sock );
        return 0;
    }
    sockaddr_in addr2;
    
    addr2.sin_family = AF_INET;
    addr2.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
    addr2.sin_port = htons( 80 );
    
    if( connect( sock, (LPSOCKADDR) &addr2, sizeof(struct sockaddr) ) == SOCKET_ERROR )
    {
        closesocket( sock );
        return 0;
    }

    HANDLE f = CreateFile( file2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );

    if( !f )
    {
        closesocket( sock );
        return 0;
    }

    char end[512] = "";
    
    wsprintf( end, "\r\n-----------------------------3164130335698\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n-----------------------------3164130335698\r\n\r\n", submit_name, submit_value );

    DWORD hi;
    DWORD size = GetFileSize( f, &hi );

    char *filepart = (char *) GlobalAlloc( GPTR, size + 2048 );

    wsprintf( filepart, "-----------------------------3164130335698\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n", file_form_name, file, mime_type );

    DWORD len = strlen( filepart );

    ReadFile( f, (LPVOID)((DWORD)filepart + len), size, &hi, NULL );

    CloseHandle( f );

    char *request = (char *) GlobalAlloc( GPTR, 1024 + len + size + 1 );

    wsprintf( request, 
    "POST /%s HTTP/1.1\r\nHost: %s\r\nUser-Agent: ELUpload/1.0\r\nConnection: Keep-alive\r\nContent-Type: multipart/form-data; boundary=---------------------------3164130335698\r\nContent-length: %d\r\n---------------------------3164130335698\r\nContent-Disposition: form-data; name=\"MAX_FILE_SIZE\"\r\n\r\n100000\r\n", 
    folder_path, url, len + size + 1 + strlen( end ) );
    
    DWORD head = strlen( request );

    memcpy( request + head, filepart, len + size + 1 );
    head += len + size + 1;
    memcpy( request + head, end, strlen( end ) );
    head += strlen( end );
    
    for( int i = 0; i < head; i++ )
    {
        if( send( sock, (request + i), 1, 0 ) == SOCKET_ERROR )
        {
            closesocket( sock );
            GlobalFree( request );
            GlobalFree( filepart );
            return 0;
        }
    }

    closesocket( sock );

    GlobalFree( request );
    GlobalFree( filepart );
    
    return 1;
}
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="text" name="serial"><br>
<input type="submit" name="submit" value="Upload File" />
</form>
Advertisements