Solved

File Upload in C lang. CGI

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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…

706 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

12 Experts available now in Live!

Get 1:1 Help Now