代码拉取完成,页面将自动刷新
#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){
cgiHeaderContentType("text/html");
cgiFilePtr file;
char name[1024];
char contentType[1024];
char buffer[1024];
int size;
int got;
if (cgiFormFileName("file", name, sizeof(name)) != cgiFormSuccess) {
printf("<p>No file was uploaded.<p>\n");
return 0;
}
fprintf(cgiOut, "The filename submitted was: ");
cgiHtmlEscape(name);
fprintf(cgiOut, "<p>\n");
cgiFormFileSize("file", &size);
fprintf(cgiOut, "The file size was: %d bytes<p>\n", size);
cgiFormFileContentType("file", contentType, sizeof(contentType));
fprintf(cgiOut, "The alleged content type of the file was: ");
cgiHtmlEscape(contentType);
fprintf(cgiOut, "<p>\n");
fprintf(cgiOut, "Of course, this is only the claim the browser made when uploading the file. Much like the filename, it cannot be trusted.<p>\n");
fprintf(cgiOut, "The file's contents are shown here:<p>\n");
if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {
fprintf(cgiOut, "Could not open the file.<p>\n");
return 0;
}
//打开或创建文件在当前目录,即/var/www/cgi-bin
//注意文件存取权限,否如会出现文件无法访问
mode_t mode=S_IRWXU|S_IRGRP|S_IROTH; //
int fd = open(name,O_RDWR|O_CREAT|O_TRUNC,mode);
if(fd < 0){
fprintf(cgiOut, "<p>Could not create the file,error:%d<p>\n",fd);
}
fprintf(cgiOut, "<pre>\n");
while (cgiFormFileRead(file, buffer, sizeof(buffer), &got) ==
cgiFormSuccess)
{
//写入文件
if(got>0){
write(fd,buffer,got);
}
// cgiHtmlEscapeData(buffer, got);
}
fprintf(cgiOut, "</pre>\n");
cgiFormFileClose(file);
close(fd);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。