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에서 파일 형식으로 변경하고 수동으로 파일 경로를 지정하려고하면 구문이 어떻게되어야합니까?
당신이 코드를 추가 할 수 있습니다
Qdecoder 업로드 파일 예? – Rob
또한 양식의 HTML입니다 (예 : method = "POST"이고 enctype = "multipart/form-data"입니까? – pce