void File()
{
cgiFilePtr file;
int fd;
mode_t mode;
char name[1024];
char contentType[1024];
char buffer[1024];
int size;
int got;
if (cgiFormFileName("upfile", name, sizeof(name)) != cgiFormSuccess) {
printf("
No file was uploaded.
");
return;
}
fprintf(cgiOut, "The filename submitted was: ");
cgiHtmlEscape(name);
fprintf(cgiOut, "
");
cgiFormFileSize("upfile", &size);
fprintf(cgiOut, "The file size was: %d bytes
", size);
cgiFormFileContentType("upfile", contentType, sizeof(contentType));
fprintf(cgiOut, "The alleged content type of the file was: ");
cgiHtmlEscape(contentType);
fprintf(cgiOut, "
");
fprintf(cgiOut, "Of course, this is only the claim the browser made when uploading the file. Much like the filename, it cannot be trusted.
");
fprintf(cgiOut, "The file's contents are shown here:
");
if (cgiFormFileOpen("upfile", &file) != cgiFormSuccess) {
fprintf(cgiOut, "Could not open the file.
");
return;
}
//打开或创建文件在当前目录,即/var/www/cgi-bin
//注意文件存取权限,否如会出现文件无法访问
mode=S_IRWXU|S_IRGRP|S_IROTH; //
fd = open(name,O_RDWR|O_CREAT|O_TRUNC,mode);
if(fd < 0){
fprintf(cgiOut, "
Could not create the file,error:%d
",fd);
}
fprintf(cgiOut, "");
while (cgiFormFileRead(file, buffer, sizeof(buffer), &got) ==
cgiFormSuccess)
{
//写入文件
if(got>0){
write(fd,buffer,got);
}
cgiHtmlEscapeData(buffer, got);
}
fprintf(cgiOut, "
");
cgiFormFileClose(file);
close(fd);
}