转自: liusong.blog.chinaunix.net
网址: http://blog.youkuaiyun.com/hurtmanzc/archive/2007/08/24/1757747.aspx
问题:
1. ubuntu中boa服务器上传文件问题。
环境:
1. 系统:ubuntu 12.04 LTS
2. boa版本:v0.94
3. cgic版本:206
解决方法:
1. 通过使用cgic库编写生成cgi,进行上传文件操作。
代码:
1. 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);
printf("name:%s<br />size:%d<br />",name,size);
//取得文件类型,不过本例中并未使用
cgiFormFileContentType("file", contentType, sizeof(contentType));
//目前文件存在于系统临时文件夹中,通常为/tmp,通过该命令打开临时文件。
//临时文件的名字与用户文件的名字不同,所以不能通过路径/tmp/userfilename的方式获得文件
if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {
fprintf(stderr,"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,"getfile/");
strcat(fileNameOnServer,name+t+1);
mode=S_IRWXU|S_IRGRP|S_IROTH;
//在当前目录下建立新的文件,第一个参数实际上是路径名,
//此处的含义是在cgi程序所在的目录(当前目录))建立新文件
targetFile=open(fileNameOnServer,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode);
if(targetFile<0){
fprintf(stderr,"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:
fprintf(stderr,"Failed to upload");
//printf("Failed to upload");
return 1;
END:
printf("File \"%s\" has been uploaded",fileNameOnServer);
return 0;
}
假设该文件存储为upload.c,则使用如下命令编辑:
gcc -Wall upload.c cgic.c -o upload.cgi
编译完成后把upload.cgi复制到你部署cgi程序的目录(通常命名为cgi-bin)。
2. html部分代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test Upload</title>
<meta name="author" content="Jack">
</head>
<body>
<form action="cgi-bin/upload.cgi" method="post" enctype="multipart/form-data" target="_blank">
<input type="file" name="file" value="" />
<input type="submit" name="submit" value="OK">
</form>
</body>
</html>
注意事项:
1. 在C代码中,下面
targetFile=open(fileNameOnServer,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode);
中的fileNameOnServer路径必须是目标文件在服务器上存储的绝对路径,或者相对于cgi程序的相对路径;并且有读写的权限,否则无法在该路径下建立上传的文件。
2. 在html代码中,下面
<form action="cgi-bin/upload.cgi" method="post" enctype="multipart/form-data" target="_blank">
中的”cgi-bin/upload.cgi”,文件名不同记得更改。
3. boa服务器有设置上传文件大小:
在进行上传文件操作时?boa服务器默认上传大小为1M,修改其上传文件大小方法2种:
1. 修改源代码的defines.h里面的宏SINGLE_POST_LIMIT_DEFAULT
# SinglePostLimit size 1024*1024*1024 = 1024M = 1G
SinglePostLimit 1073741824
2. 修改boa.conf里面的SinglePostLimit
4. 上传文件大小控制:
那么如何控制上传文件的大小呢?因为你有时会不允许用户上传太大的文件。
通过分析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 */
}
}
变量UpSize表示文件大小的上限。在cgic.c的main中找到相关代码,并修改成上面这样即可。你可以在cgic.c中定义UpSize,也可以在刚才完成的upload.c中定义,然后在cgic.c中用extern方式引用。
下载链接:
声明:
本文出处:http://blog.youkuaiyun.com/hurtmanzc/archive/2007/08/24/1757747.aspx