⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cgictest.c

📁 使用cgi实现文件的上传
💻 C
字号:
/* Change this if the SERVER_NAME environment variable does not report	the true name of your web server. */#if 1#define SERVER_NAME cgiServerName#endif#if 0#define SERVER_NAME "www.boutell.com"#endif/* You may need to change this, particularly under Windows;	it is a reasonable guess as to an acceptable place to	store a saved environment in order to test that feature. 	If that feature is not important to you, you needn't	concern yourself with this. */#ifdef WIN32#define SAVED_ENVIRONMENT "c:\\cgicsave.env"#else#define SAVED_ENVIRONMENT "/tmp/cgicsave.env"#define SAVED_UPLOAD "./"#endif /* WIN32 */#include <stdio.h>#include "cgic.h"#include <string.h>#include <stdlib.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>void HandleSubmit();void CookieSet();void File();int cgiMain() {	/* Set any new cookie requested. Must be done *before*		outputting the content type. */	CookieSet();	/* Send the content type, letting the browser know this is HTML */	cgiHeaderContentType("text/html");	/* Top of the page */	fprintf(cgiOut, "<HTML><HEAD>\n");	fprintf(cgiOut, "<TITLE>Software Update</TITLE></HEAD>\n");	//fprintf(cgiOut, "<BODY><H1>cgic test</H1>\n");	/* If a submit button has already been clicked, act on the 		submission of the form. */	if (cgiFormSubmitClicked("testcgic") == cgiFormSuccess)	{		HandleSubmit();	//	fprintf(cgiOut, "<hr>\n");	}	/* Now show the form */	//ShowForm();	/* Finish up the page */	fprintf(cgiOut, "</BODY></HTML>\n");	return 0;}void HandleSubmit(){	File();}void File(){	cgiFilePtr file;	char name[1024];	char *ptrchar = name;	char *p;//	char contentType[1024];	char buffer[1024];	//int size;	int got;	int dog=1;	int count=1;	char filename[256];	int fd, readlength;	if (cgiFormFileName("file", name, sizeof(name)) != cgiFormSuccess) {		printf("<p>No file was uploaded.<p>\n");		return;	} //	fprintf(cgiOut, "The filename submitted was: ");//	cgiHtmlEscape(name);		while(dog){		if(*(ptrchar+strlen(name)-count) == '\\') 		{			dog = 0;		}		count++;	}	p = filename;    p += sprintf(p,"%s",SAVED_UPLOAD);	strncpy(p ,(ptrchar+strlen(name)-count+2),count-1);	printf("<p>he filename is %s <p>",filename);	#if 0	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");	#endif 		if (cgiFormFileOpen("file", &file) != cgiFormSuccess) {		fprintf(cgiOut, "Could not open the file.<p>\n");		return;	}	/*	int fd2 = open("ttttttt", O_CREAT | O_TRUNC | O_WRONLY, 0755);	if(fd2 < 0)		printf("<p>dsafas klasdjfldjlkfjasdlkdklsjaflkasdj  fail to creat upload file<p>");	write(fd2, p, strlen(p));		close(fd2);	*/	//chdir(SAVED_UPLOAD);	//system("rm -rf /mnt/mpg/imhttpd");	//system("killall imhttpd");	//	sync() ;	sleep(3);	/*Create a upload file*/		if((fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0755)) < 0){		printf("fail to creat upload file\n");		exit(0);		}			while (cgiFormFileRead(file, buffer, sizeof(buffer), &got) ==		cgiFormSuccess)	{		readlength = write(fd, buffer, got);		//	cgiHtmlEscapeData(buffer, got);		}	fprintf(cgiOut, "<p>\n");	fprintf(cgiOut, "<p>\n");	if (fd > 0)		fprintf(cgiOut, "<font face=\"Arial, Helvetica, sans-serif\" size=\"2\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Uploading ...... Success!</b></font>\n");	else		fprintf(cgiOut, "<font face=\"Arial, Helvetica, sans-serif\" size=\"2\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Uploading ...... Failure!</b></font>\n");		//	fprintf(cgiOut, "<>\n");	close(fd);	cgiFormFileClose(file);}void CookieSet(){	char cname[1024];	char cvalue[1024];	/* Must set cookies BEFORE calling cgiHeaderContentType */	cgiFormString("cname", cname, sizeof(cname));		cgiFormString("cvalue", cvalue, sizeof(cvalue));		if (strlen(cname)) {		/* Cookie lives for one day (or until browser chooses			to get rid of it, which may be immediately),			and applies only to this script on this site. */			cgiHeaderCookieSetString(cname, cvalue,			86400, cgiScriptName, SERVER_NAME);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -