====== 用c语言写cgi程序(3)---实现文件上传 ======
分类: 嵌入式网络 2010-03-09 11:11 1942人阅读 评论(2) 收藏 举报
敬告:其实当前的cgic版本已经有上传的功能了,可以看看自带的test文件
用C语言编写cgi程序的话,多半会用到CGIC。 这是个非常流行的库,遇到文件上传之类的应用更是离不开它。官方页面及下载地址为:www.boutell.com/cgic/#obtain
不少网站都有文件上传的功能,本文展示如何用CGIC库编写文件上传的服务端程序,最后给出一段简单的HTML代码,供大家测试使用 。
#include
#include
#include
#include
#include
#include"cgic.h"
#define BufferLen 1024
int cgiMain(void){
cgiFilePtr file;
int targetFile;
mode_t mode;
char name[128];
char todir[30]="/tmp/";
char fileNameOnServer[64];
char contentType[1024];
char buffer[BufferLen];
char *tmpStr=NULL;
int size;
int got,t;
cgiHeaderContentType("text/html");
if (cgiFormFileName("file", name, sizeof(name)) != cgiFormSuccess) {
//printf("could not retrieve filename\n");
goto FAIL;
}
cgiFormFileSize("file", &size);
cgiFormFileContentType("file", contentType, sizeof(contentType));
if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {
//printf("could not open the file\n");
goto FAIL;
}
t=-1;
while(1){
tmpStr=strstr(name+t+1,"\\");
if(NULL==tmpStr)
tmpStr=strstr(name+t+1,"/");//if "\\" is not path separator, try "/"
if(NULL!=tmpStr)
t=(int)(tmpStr-name);
else
break;
}
strcpy(fileNameOnServer,name+t+1);
strcat(todir,fileNameOnServer);
//printf("%s\n\n",todir);
mode=S_IRWXU|S_IRGRP|S_IROTH;
//targetFile=open(fileNameOnServer,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode);
//
targetFile=open(todir,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode);
if(targetFile<0){
//printf("could not create the new file,%s\n",fileNameOnServer);
goto FAIL;
}
while (cgiFormFileRead(file, buffer, BufferLen, &got) ==cgiFormSuccess){
if(got>0)
write(targetFile,buffer,got);
}
cgiFormFileClose(file);
close(targetFile);
goto END;
FAIL:
//printf("Failed to upload");
return 1;
END:
//printf("OK!! File \"%s\" has been uploaded",fileNameOnServer);
printf("");
return 0;
}
假设该文件存储为upload.c,则使用如下命令编辑:
gcc -Wall upload.c cgic.c -o upload.cgi
编译完成后把upload.cgi复制到你部署cgi程序的目录(通常命名为cgi-bin)。
正式部署时,请务必修改用open创建新文件那一行代码。把open的第一个参数设置为目标文件在服务器上存储的绝对路径,或者相对于cgi程序的相对路径。本例中,出于简单考虑,在cgi程序所在目录下创建新文件。
测试用HTML代码:
Test Upload
最后的文件目录结构为
/MyWebRoot
|—/upload.html
|—/cgi-bin
|——/upload.cgi
当然,你必须配置能够cgi-bin,并且程序要有权限在cgi-bin目录下创建文件(因为此例把文件上传到cgi-bin目录下)。
那么如何控制上传文件的大小呢?因为你有时会不允许用户上传太大的文件。
通过分析cgic.c的源代码,我们发现它定义了一个变量cgiContentLength,表示请求的长度。但我们需要首先判断这是一个上传文件的请求,然后才能根据cgiContentLength来检查用户是否要上传一个太大的文件。
cgic.c的main函数中进行了一系列if-else判断来检查请求的类型,首先确定这是一个post请求,然后确定数据的编码方式为 “multipart/form-data”,这个判断通过之后,就要开始准备接收数据了。所以我们要在接收数据开始之前使用 cgiContentLength判断大小,如果超过标准,就立即返回,不允许继续操作。
下面贴出修改后代码片段(直接修改cgic.c的源代码即可):
else if (cgiStrEqNc(cgiContentType, "multipart/form-data")) {
#ifdef CGICDEBUG
CGICDEBUGSTART
fprintf(dout, "Calling PostMultipartInput/n");
CGICDEBUGEND
#endif /* CGICDEBUG */
//我的代码
//UpSize:文件长度上限值,以byte为单位,UpSize是一个int变量,因为cgiContentLength的类型为int
if(cgiContentLength>UpSize){
cgiHeaderContentType("text/html");
printf("File too large!/n");
cgiFreeResources();
return -1;
}
//我的代码结束
if (cgiParsePostMultipartInput() != cgiParseSuccess) {
#ifdef CGICDEBUG
CGICDEBUGSTART
fprintf(dout, "PostMultipartInput failed/n");
CGICDEBUGEND
#endif /* CGICDEBUG */
cgiFreeResources();
return -1;
}
#ifdef CGICDEBUG
CGICDEBUGSTART
fprintf(dout, "PostMultipartInput succeeded/n");
CGICDEBUGEND
#endif /* CGICDEBUG */
}
}