CGI c文件打开

我在c中创建了一个cgi文件。 我有一个html文件,用户可以在其中选择他/她想要上传的文件。

问题是,当我选择文件时,文件的值只是文件的名称而不是目录,所以我无法读取文件。

我该如何访问该文件?

Title: 
Author: 
File: 

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; } 

主要代码

  // 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); } 

即使我使用POST方法,问题仍然存在。 问题不在于如何解析html的输入。 问题是当我试图在主代码中试图。

如果我将html中的类型从文件更改为文本并尝试手动提供文件的路径,那么语法应该如何呢?

您不需要目录信息,文件内容附加到POST请求,您需要从那里获取它。 我建议你使用cgic和qdecoder作为CGI库,为你处理这个过程。 或者您至少可以理解它们如何解析请求标头,并可能在您自己的应用程序中应用相同的逻辑。

Qdecoder上传文件示例: http ://www.qdecoder.org/releases/current/examples/uploadfile.c