basic function to upload file to http server

Anything and everything related to network programming

basic function to upload file to http server

Postby reflection » Sun Feb 28, 2010 10:50 am

Well, this is just a POC code that shows how to upload file to HTTP server.

here is the php code
Code: Select all
<b>Uploader</b><br>
<form action='upload.php' method=post   enctype='multipart/form-data'>
<input type='file' name='filename'><br>
<input type='hidden' name='file' value='upload'>
<input type='submit' value='Upload File'>
</form>
<?php

if ($_POST["file"] == 'upload')
{
copy($_FILES["filename"]["tmp_name"],"./files/".$_FILES["filename"]["name"]) or die("Couldn't copy file");
echo "<b>File Uploaded.</b>";
}   
?>



And here is the code that do the actual uploading.
2 different functions, getFile returns the file's data(when you need to upload big files you shouldn't use function that returns the file data like i did but read some data and send it to the server, and then again read some data and send it to the server untill the file ends, i originaly wrote this code to upload little text file so this method was good enough for me)
and sendFileToHTTP uploads the file.
it takes 3 parameters:
the host, the page location and the location of the file you would like to upload.

Code: Select all

wchar_t *getFile(const char *szFile)
{
      HANDLE hFile=CreateFile(szFile,
         GENERIC_READ,
         FILE_SHARE_READ,
         NULL,
         OPEN_EXISTING,
         NULL,
         NULL);
      int num;
      BYTE *tmp;
      DWORD dwRead;


      if(hFile==INVALID_HANDLE_VALUE)
         return NULL;
      

      num=GetFileSize(hFile,NULL);
      tmp=(BYTE*)malloc(sizeof(BYTE) * num);
   
      ReadFile(hFile,(LPVOID)tmp,num,&dwRead,NULL);
   
      CloseHandle(hFile);

   return (wchar_t*)tmp;
}


BOOL sendFiletoHTTP(char *szHost,char *szPage,char *szFile)
{
   int sock,tmpsock=SOCKET_ERROR;
   struct sockaddr_in sin;   
   struct hostent *he=gethostbyname(szHost);
   int len,sent=0,ret;
   char *req;
   char x[100000];
   wchar_t *data=(wchar_t*)getFile(szFile);



   if(data==NULL)
      return FALSE;

   sock = socket(AF_INET, SOCK_STREAM, 0);
   if (sock < 1)
      return FALSE;

   sin.sin_family = AF_INET;
   sin.sin_port = htons(80);
   sin.sin_addr = *((struct in_addr *)he->h_addr);
   memset(&(sin.sin_zero), '\0', 8);
   if(connect(sock,(struct sockaddr*)&sin,16)==-1)
      return FALSE;


   req=(char*)malloc(10000 *sizeof(char));
   sprintf(req,"POST /%s HTTP/1.1\r\n"
   "Host: %s\r\n"
   "Content-Type: multipart/form-data; boundary=106282385829839\r\n"
   "Content-Length: %d\r\n\r\n"
   "--106282385829839\r\n"
   "Content-Disposition: form-data; name=\"MAX_FILE_SIZE\"\r\n\r\n"
   "100000\r\n"
   "--106282385829839\r\n"
   "Content-Disposition: form-data; name=\"filename\"; filename=\"%s\"\r\n"
   "Content-Type: text/plain charset=utf-8\r\n"
   "Content-Transfer-Encoding: binary\r\n\r\n",szPage,szHost,(wcslen(data)*2+strlen(szFile)+254+14),szFile);


   len=strlen(req);
   while(sent<len)
   {
      ret=send(sock,req+sent,len,0);
      sent+=ret;
   }
   
   sent=0;
   len=wcslen(data)*2;

   while(sent<len)
   {   
      ret=send(sock,(char*)data+sent,len,0);
      sent+=ret;
   }

   strcpy(req,"\r\n--106282385829839--\r\n\r\n\r\n");
   len=strlen(req);
   sent=0;

   while(sent<len)
   {
   ret=send(sock,req+sent,len,0);
   sent+=ret;
   }
   while(tmpsock==SOCKET_ERROR)
   {
      tmpsock=recv(sock,x,10000,0);

   }
   x[tmpsock]=NULL;
   printf(x);
   free(data);
   free(req);
   closesocket(sock);
   return TRUE;


}


have fun!
reflection
Newcomer
 
Posts: 7
Joined: Sat Feb 27, 2010 2:58 am

Re: basic function to upload file to http server

Postby lp rob1 » Mon Mar 15, 2010 2:50 am

Sounds interesting... but isn't that what the members area is for? It would be quite useful to be able to upload files to a home http server while away, so there is some practicality in it :lol:

See if you can make a get file and a send function that uses chunks of files, like you said:
reflection wrote:2 different functions, getFile returns the file's data(when you need to upload big files you shouldn't use function that returns the file data like i did but read some data and send it to the server, and then again read some data and send it to the server untill the file ends, i originaly wrote this code to upload little text file so this method was good enough for me)


Could be a community project...
User avatar
lp rob1
Veteren
 
Posts: 125
Joined: Tue Jan 12, 2010 6:34 am
Location: England, UK


Return to Winsock And Networking

Who is online

Users browsing this forum: No registered users and 1 guest

cron