【问题标题】:CGI c file openCGI c文件打开
【发布时间】:2014-02-16 05:41:44
【问题描述】:

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

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

我应该如何访问该文件?

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

主要代码

     // 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中的类型从文件更改为文本并尝试手动给出文件的路径,语法应该如何?

【问题讨论】:

  • 还有表单的 HTML,例如。有 method="POST" 和 enctype="multipart/form-data" 吗?

标签: c file cgi


【解决方案1】:

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

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

【讨论】:

  • 不幸的是@Olga V. Sungutay 我不允许使用 cgic 或类似的东西,因为我正在为我学校的一门课程做作业。愚蠢的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多