Solved

File Upload in C lang. CGI

Posted on 1998-01-15
3
811 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

856 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