前言:
用C语言编写cgi程序的话,CGIC是非常流行的库,官方页面及下载地址为:www.boutell.com/cgic/#obtain;
不少网站都有文件上传的功能,本文展示如何用CGIC库编写文件上传的服务端程序,最后给出一段简单的HTML代码,供大家测试使用。
一、cgi程序:
//upload.c
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include"cgic.h"
#define BufferLen 1024
int cgiMain(void){
cgiFilePtr file;
int targetFile;
mode_t mode;
char name[128];
char fileNameOnServer[64];
char contentType[1024];
char buffer[BufferLen];
char *tmpStr=NULL;
int size;
int got,t;
cgiHeaderContentType("text/html");
//取得html页面中file元素的值,应该是文件在客户机上的路径名
if (cgiFormFileName("file", name, sizeof(name)) !=cgiFormSuccess) {
fprintf(stderr,"could not retrieve filename\n");
goto FAIL;
}
cgiFormFileSize("file", &size);
//取得文件类型,不过本例中并未使用
cgiFormFileContentType("file", contentT