您的位置:首页 > 其它

cgi实现网页向服务器传送文件

2016-11-03 17:06 204 查看
linux系统,boa服务器,借助UltraEdit软件。

主要原理就是在缓冲区里定位到RAR压缩文件的头尾,并写入到文件中

代码如下

/*************************a.c***********************/

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <fcntl.h>

#include <unistd.h>

using namespace std;

 

#define MAX_CONTENT_LEN        1024

char szContent[MAX_CONTENT_LEN];

char *p,*q;

int main ()

{

   int i,nlen;

   cout << "Content-type:text/html\n\r\n";

   cout << "<html>\n";

   cout << "<head>\n";

   cout << "<title>CGI 环境变量</title>\n";

   cout << "</head>\n";

   cout << "<body>\n";

   

     if ( strcmp("POST", getenv("REQUEST_METHOD")) == 0 )

    {

        nlen = atoi(getenv("CONTENT_LENGTH"));

        for (i = 0; i < nlen; i++ )    

        {

            if ( i < MAX_CONTENT_LEN )

                szContent[i] = fgetc(stdin);

            else

                break;

        }

        printf("<p>%s</p>", szContent);
for (i = 0; i < nlen; i++ )  
printf("%x ", (unsigned char)szContent[i]);

    }

p = szContent;
p = (6+4+strstr(p,"stream"));
q = szContent+nlen-46;
int fd = open("2.rar",O_RDWR|O_CREAT,777);
write(fd,p,(int)(q-p));
close(fd);

   cout << "</body>\n";

   cout << "</html>\n";

   

   return 0;

}

/**************************1.html************************/

<html>

<body>

   <form enctype="multipart/form-data" 

            action="/cgi-bin/a.out" 

            method="post">

   <p>文件:<input type="file" name="userfile" /></p>

   <p><input type="submit" value="上传" /></p>

   </form>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: