Solved

File Upload in C lang. CGI

Posted on 1998-01-15
3
818 Views
Last Modified: 2013-12-20
Dear experts...

I'm in messy about file upload...

In Netscape HTML <type="file"> can works good..

but my server side CGI(wriiten by C) is not recognize

it's size and boundary and so on...

So I want to request sample file upload CGI source
written in C.

and I am not accustomed to Perl scripts...

so please tell me how can I upload files by C CGI....

please help me...

thank u...
0
Comment
Question by:taijin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:taijin
ID: 1831833
Edited text of question
0
 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 1831834
Here is an example of doing an upload in C:


/* Accept a single file from a Web Brower file upload form */
/* and store the data received in /tmp/MyFile              */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
      FILE *fp;
      char *request_method;
      char *content_length;
      char *content_type;
      char *boundary, *pchar;
      char *pschar, *pechar;
      char *delim = "\r\n";
      char *datastart = "\r\n\r\n";

      int data_len, data_read, i, j, found;
      char *buffer, *filedata;

      printf("Content-type: text/html\n\n");

      request_method = getenv("REQUEST_METHOD");
      content_length = getenv("CONTENT_LENGTH");
      content_type = getenv("CONTENT_TYPE");

      if(request_method == NULL){
            printf("<center>Error: Unknown cgi-bin request</center>\n");
            printf("</html>\n");
            return;
      }

      /* This only handles POST */
      if(strcmp(request_method, "POST") != 0){
            printf("<center>Error: %s request method not supported</center>\n",
                  request_method);
            printf("</html>\n");
            return;            
      }

      printf("<p>REQUEST_METHOD = %s</p>\n", request_method);
      printf("<p>CONTENT_LENGTH = %s</p>\n", content_length);
      printf("<p>CONTENT_TYPE = %s</p>\n", content_type);

      /* The total length of what the client sent upstream */
      data_len = atoi(content_length);

      /*
         Make a space to hold all of the data.  If large uploads are expected
           and memory is limited, this should be done in blocks rather than all
           at once
        */
      buffer = (char *)malloc(data_len);
      data_read = fread((void *)buffer, 1, data_len, stdin);

      printf("<p>Read %d bytes (of %d) from stdin</p>\n", data_read, data_len);

      /* Get the boundary marker for start/end of the data */
      pchar = strtok(buffer, delim);
      boundary = strdup(buffer);
      
      /* Find the data start */
      pschar = strstr(buffer+strlen(boundary)+1, datastart);
      if(pschar == NULL){
            printf("<p>ERROR - BAD DATA START</p>\n");
            for(i=0; i<10; i++){
                  printf("%c ", *(buffer+strlen(boundary)+i));
            }
            return;
      }

      /* Move the pointer past the end of headers marker */
      pschar+=strlen(datastart);

      /* Find the data end */
        /* Can't use strstr here as the data may be binary */
      found = 0;
      for(pchar=pschar; pchar < buffer+data_len-strlen(boundary); pchar++){
            if(memcmp(pchar, boundary, strlen(boundary)) == 0){
                  found = 1;
                  break;
            }
      }

      if(found == 1){
            pechar = pchar;
      }
      else{
            printf("<p>ERROR - BAD DATA END</p>\n");
            free(boundary);
            free(buffer);
            return;
      }

      /* Write the data to a file */
      fp = fopen("/tmp/MyFile", "w");
      if(fp == NULL){
            printf("<p>Error opening output file for write</p>\n");
      }
      else{
            fwrite(pschar, 1, pechar-pschar, fp);
            fclose(fp);
            printf("<p>Data Written to file</p>\n");
      }

      free(boundary);
      free(buffer);
      
      printf("</html>\n");      
}
0
 

Author Comment

by:taijin
ID: 1831835
Thank u so much jhance...

Your answer is very helpful to me...

thanks again..

and I have found some source in C about file upload...

TANDEM web server has some file upload examples in C CGI...

below is the URL about that....

http://oss2.tandem.com:9618/web-upload/

Thanks to jhance.. and TANDEM... and all of the WORLD...

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question