CGI C文件打开 [英] CGI c file open

查看:205
本文介绍了CGI C文件打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立在C CGI文件。我有一个html文件中,用户可以选择他/她想要上传的文件。

I have created a cgi file in c. I have an html file in which the user can select the file he/she wants to upload.

问题是,当我选择的文件,文件的值是文件的仅仅是名称,而不是目录,所以我不能读取文件。

The problem is that when I select the file, the value of the file is just the name of the file and not the directory so I cannot read the file.

我应该如何访问文件?

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

Ç - CGI code

c - cgi code

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

主要code

     // 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输入。问题是,当我尝试FREAD主code。

Even If i use POST method the problem still exists. The issue is not how to parse the input from html. The problem is when I try to fread in the main code.

如果我更改从HTML文件类型为文本,并尝试手动给文件的路径,如何应该是语法?

If I change the type in html from file to text and try to give the path of the file manually, how should be the syntax?

推荐答案

您不需要的目录信息,文件内容附加到POST请求,你需要从那里抓住它。
我建议你​​使用 CGIC 并的 QDE codeR 作为照顾的过程中为你的CGI库。或者,你至少可以明白它们是如何解析请求头,也许套用同样的逻辑在自己的应用程序。

You don't need the directory information, the file content is attached to the POST request and you need to grab it from there. I suggest you use cgic and qdecoder as CGI libraries that take care of the process for you. Or you can at least understand how they are parsing the request headers and maybe apply the same logic in your own application.

QDE codeR上传文件,例如:的http:/ /www.qde$c$cr.org/releases/current/examples/uploadfile.c

Qdecoder Upload file example: http://www.qdecoder.org/releases/current/examples/uploadfile.c

这篇关于CGI C文件打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆