CGI C上傳文件以及大小限制修改

CGI C上傳文件以及大小限制修改  

BOA+CGIC,發現用它那個測試程序上傳文件時只能傳1MB大小左右,後來搜索網絡,發現是BOA搞的鬼,方法2種:

1、修改源代碼的defines.h裏面的宏SINGLE_POST_LIMIT_DEFAULT

2、修改boa.conf裏面的SinglePostLimit

cgic文件命令爲: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,"./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");
    return 1;
END:    
    printf("File \"%s\" has been uploaded",fileNameOnServer);
    return 0;
}

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>

編譯:gcc -Wall upload.c cgic.c -o upload.cgi

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章