用c语言写cgi程序(3)---实现文件上传

来源:互联网 发布:java ftp上传附件 编辑:程序博客网 时间:2024/05/17 21:40

敬告:其实当前的cgic版本已经有上传的功能了,可以看看自带的test文件

用C语言编写cgi程序的话,多半会用到CGIC。 这是个非常流行的库,遇到文件上传之类的应用更是离不开它。官方页面及下载地址为:www.boutell.com/cgic/#obtain

不少网站都有文件上传的功能,本文展示如何用CGIC库编写文件上传的服务端程序,最后给出一段简单的HTML代码,供大家测试使用 。

下载: upload.c
[cpp] view plaincopy
  1. #include<stdio.h>   
  2. #include<string.h>   
  3. #include<unistd.h>   
  4. #include<fcntl.h>   
  5. #include<sys/stat.h>   
  6. #include"cgic.h"   
  7. #define BufferLen 1024   
  8. int cgiMain(void){   
  9. cgiFilePtr file;   
  10. int targetFile;   
  11. mode_t mode;   
  12. char name[128];   
  13. char fileNameOnServer[64];   
  14. char contentType[1024];   
  15. char buffer[BufferLen];   
  16. char *tmpStr=NULL;   
  17. int size;   
  18. int got,t;   
  19. cgiHeaderContentType("text/html");   
  20. //取得html页面中file元素的值,应该是文件在客户机上的路径名   
  21. if (cgiFormFileName("file", name, sizeof(name)) !=cgiFormSuccess) {   
  22. fprintf(stderr,"could not retrieve filename/n");   
  23. goto FAIL;   
  24. }   
  25. cgiFormFileSize("file", &size);   
  26. //取得文件类型,不过本例中并未使用   
  27. cgiFormFileContentType("file", contentType, sizeof(contentType));   
  28. //目前文件存在于系统临时文件夹中,通常为/tmp,通过该命令打开临时文件。临时文件的名字与用户文件的名字不同,所以不能通过路径/tmp/userfilename的方式获得文件   
  29. if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {   
  30. fprintf(stderr,"could not open the file/n");   
  31. goto FAIL;   
  32. }   
  33. t=-1;   
  34. //从路径名解析出用户文件名   
  35. while(1){   
  36. tmpStr=strstr(name+t+1,"//");   
  37. if(NULL==tmpStr)   
  38. tmpStr=strstr(name+t+1,"/");//if "//" is not path separator, try "/"   
  39. if(NULL!=tmpStr)   
  40. t=(int)(tmpStr-name);   
  41. else   
  42. break;   
  43. }   
  44. strcpy(fileNameOnServer,name+t+1);   
  45. mode=S_IRWXU|S_IRGRP|S_IROTH;   
  46. //在当前目录下建立新的文件,第一个参数实际上是路径名,此处的含义是在cgi程序所在的目录(当前目录))建立新文件   
  47. targetFile=open(fileNameOnServer,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode);   
  48. if(targetFile<0){   
  49. fprintf(stderr,"could not create the new file,%s/n",fileNameOnServer);   
  50. goto FAIL;   
  51. }   
  52. //从系统临时文件中读出文件内容,并放到刚创建的目标文件中   
  53. while (cgiFormFileRead(file, buffer, BufferLen, &got) ==cgiFormSuccess){   
  54. if(got>0)   
  55. write(targetFile,buffer,got);   
  56. }   
  57. cgiFormFileClose(file);   
  58. close(targetFile);   
  59. goto END;   
  60. FAIL:   
  61. fprintf(stderr,"Failed to upload");   
  62. return 1;   
  63. END:   
  64. printf("File /"%s/" has been uploaded",fileNameOnServer);   
  65. return 0;   
  66. }   

假设该文件存储为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的源代码即可):

 

[cpp] view plaincopy
  1. else if (cgiStrEqNc(cgiContentType, "multipart/form-data")) {   
  2. #ifdef CGICDEBUG   
  3. CGICDEBUGSTART   
  4. fprintf(dout, "Calling PostMultipartInput/n");   
  5. CGICDEBUGEND   
  6. #endif /* CGICDEBUG */   
  7. //我的代码   
  8. //UpSize:文件长度上限值,以byte为单位,UpSize是一个int变量,因为cgiContentLength的类型为int   
  9. if(cgiContentLength>UpSize){   
  10. cgiHeaderContentType("text/html");   
  11. printf("File too large!/n");   
  12. cgiFreeResources();   
  13. return -1;   
  14. }   
  15. //我的代码结束   
  16. if (cgiParsePostMultipartInput() != cgiParseSuccess) {   
  17. #ifdef CGICDEBUG   
  18. CGICDEBUGSTART   
  19. fprintf(dout, "PostMultipartInput failed/n");   
  20. CGICDEBUGEND   
  21. #endif /* CGICDEBUG */   
  22. cgiFreeResources();   
  23. return -1;   
  24. }   
  25. #ifdef CGICDEBUG   
  26. CGICDEBUGSTART   
  27. fprintf(dout, "PostMultipartInput succeeded/n");   
  28. CGICDEBUGEND   
  29. #endif /* CGICDEBUG */   
  30. }   

0 0