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