Solved

File Upload in C lang. CGI

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now