#include #include #include #include #include #include"cgic.h" #define BufferLen 1024 int cgiMain(void){ cgiFilePtr file; int targetFile; mode_t mode; char name[128]; char todir[30]="/tmp/"; char fileNameOnServer[64]; char contentType[1024]; char buffer[BufferLen]; char *tmpStr=NULL; int size; int got,t; cgiHeaderContentType("text/html"); if (cgiFormFileName("file", name, sizeof(name)) != cgiFormSuccess) { //printf("could not retrieve filename\n"); goto FAIL; } cgiFormFileSize("file", &size); cgiFormFileContentType("file", contentType, sizeof(contentType)); if (cgiFormFileOpen("file", &file) != cgiFormSuccess) { //printf("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,name+t+1); strcat(todir,fileNameOnServer); //printf("%s\n\n",todir); mode=S_IRWXU|S_IRGRP|S_IROTH; //targetFile=open(fileNameOnServer,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode); // targetFile=open(todir,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode); if(targetFile<0){ //printf("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: //printf("Failed to upload"); return 1; END: //printf("OK!! File \"%s\" has been uploaded",fileNameOnServer); printf(""); return 0; }