Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

File Upload in C lang. CGI

Posted on 1998-01-15
3
Medium Priority
?
827 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
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…
Suggested Courses

604 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