2012-10-29 3 views
0

c에서 cgi 파일을 만들었습니다. 사용자가 업로드 할 파일을 선택할 수있는 html 파일이 있습니다.CGI 파일 열기

문제는 내가 파일을 선택할 때 파일을 읽을 수 없으므로 파일의 값은 파일 이름이 아니라 디렉터리 이름입니다.

파일에 어떻게 액세스해야합니까?

<form action="./cgi-bin/paperload.cgi" method="get"> 
       <pre>Title: <input type="text" name="title"><br></pre> 
       <pre>Author: <input type="text" name="author"><br></pre> 
       <pre>File: <input type="file" name="file"><br></pre> 
       <pre><input type="submit" value="Upload paper"></pre> 
      </form> 

C - CGI 코드

void getParam(const char *Name, char Value[256]) { 
    char *pos1 = strstr(data, Name); 

    if (pos1) { 
    pos1 += strlen(Name); 
    if (*pos1 == '=') { // Make sure there is an '=' where we expect it 
     pos1++; 
     while (*pos1 && *pos1 != '&') { 
      if (*pos1 == '%') { // Convert it to a single ASCII character and store at our Valueination 
       *Value++ = (char)ToHex(pos1[1]) * 16 + ToHex(pos1[2]); 
       pos1 += 3; 
      } else if(*pos1=='+') { // If it's a '+', store a space at our Valueination 
      *Value++ = ' '; 
      pos1++; 
      } else { 
      *Value++ = *pos1++; // Otherwise, just store the character at our Valueination 
       } 
     } 

      *Value++ = '\0'; 
      return; 
     } 
     } 

    strcpy(Value, "undefine"); // If param not found, then use default parameter 
    return; 
} 

주요 코드 내가 문제가 여전히 존재 POST 방법을 사용하는 경우에도

 // check for a correct id 
data = getenv("QUERY_STRING"); 

getParam("title",paper->author_name); 
getParam("author",paper->paper_title); 

getParam("file",paper->paper_file_name); 
paper_file = fopen(paper->paper_file_name, "r+"); 
if (paper_file) { 

      // we continue to read until we reach end of file 
      while(!feof(paper_file)){ 
       fread(paper->paper_content,BUFSIZ, 1, paper_file); 
      } 
      //close the file 
      fclose(paper_file); 
     } 
     else { 
      fprintf(stderr, "Unable to open file %s", paper->paper_file_name); 
      exit(-1); 
     } 

. 문제는 HTML에서 입력을 구문 분석하는 방법이 아닙니다. 문제는 내가 주 코드에서 fread하려고 할 때이다.

형식을 HTML에서 파일 형식으로 변경하고 수동으로 파일 경로를 지정하려고하면 구문이 어떻게되어야합니까?

+0

당신이 코드를 추가 할 수 있습니다

Qdecoder 업로드 파일 예? – Rob

+0

또한 양식의 HTML입니다 (예 : method = "POST"이고 enctype = "multipart/form-data"입니까? – pce

답변

1

디렉토리 정보가 필요하지 않으며 파일 내용이 POST 요청에 첨부되어 있으며 거기에서 가져와야합니다. 난 당신을 위해 처리하는 CGI 라이브러리로 cgicqdecoder을 사용하시기 바랍니다. 또는 적어도 요청 헤더를 파싱하는 방법을 이해하고 자신의 응용 프로그램에 동일한 논리를 적용 할 수 있습니다. http://www.qdecoder.org/releases/current/examples/uploadfile.c

+0

불행히도 @Olga V. Sungutay 나는 학교 수업을 배정하기 때문에 나는 cgic 나 비슷한 것을 사용할 수 없다. 어리석은 것들. –