用C語言編寫cgi程序的話,CGIC是非常流行的庫,官方頁面及下載地址為:www.boutell.com/cgic/#obtain
不少網站都有文件上傳的功能,本文展示如何用CGIC庫編寫文件上傳的服務端程序,最后給出一段簡單的HTML代碼,供大家測試使用。
//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", 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,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");
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)。
正式部署時,請務必修改用open創建新文件那一行代碼。把open的第一個參數設置為目標文件在服務器上存儲的絕對路徑,或者相對于cgi程序的相對路徑。本例中,出于簡單考慮,在cgi程序所在目錄下創建新文件。
測試用HTML代碼: upload.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">
<!-- Date: 2007-08-30 -->
</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>
最后的文件目錄結構為
/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 */
}
}
變量UpSize表示文件大小的上限。在cgic.c的main中找到相關代碼,并修改成上面這樣即可。你可以在cgic.c中定義UpSize,也可以在剛才完成的upload.c中定義,然后在cgic.c中用extern方式引用。
support my English blog: http://www.phacai.com/how-to-implement-file-upload-with-cgic

]]>