Reprint: Click to enter the github project address
After searching for the code of uploading files to the server for a long time on the internet, I basically found the same code. Here's the code
#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) { cgiFilePtr file; int targetFile; mode_t mode; char name[128]; char fileNameOnServer[64]; char contentType[1024]; char buffer[BufferLen]; char *tmpStr=NULL; int size; int got,t; cgiHeaderContentType("text/html"); //Get the value of the file element in the html page, which should be the path name of the file on the client if (cgiFormFileName("file", name, sizeof(name)) !=cgiFormSuccess) { fprintf(stderr,"could not retrieve filename/n"); goto FAIL; } cgiFormFileSize("file", &size); //Get the file type, but it is not used in this example cgiFormFileContentType("file", contentType, sizeof(contentType)); //Currently the file exists in the system temporary folder, usually / tmp, which opens the temporary file. Temporary files have different names from user files, so files cannot be obtained by path / tmp/userfilename if (cgiFormFileOpen("file", &file) != cgiFormSuccess) { fprintf(stderr,"could not open the file/n"); goto FAIL; } t=-1; //Resolving User File Name from Path Name 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); mode=S_IRWXU|S_IRGRP|S_IROTH; //To create a new file in the current directory, the first parameter is actually the path name, which means to create a new file in the directory where the cgi program is located (the current directory). targetFile=open(fileNameOnServer,O_RDWR|O_CREAT|O_TRUNC|O_APPEND,mode); if(targetFile<0) { fprintf(stderr,"could not create the new file,%s/n",fileNameOnServer); goto FAIL; } //Read the file content from the system temporary file and put it in the newly created target file while (cgiFormFileRead(file, buffer, BufferLen, &got) ==cgiFormSuccess) { if(got>0) write(targetFile,buffer,got); } cgiFormFileClose(file); close(targetFile); goto END; FAIL: fprintf(stderr,"Failed to upload"); return 1; END: printf("File %s has been uploaded",fileNameOnServer); return 0; }
Basically, what I'm looking for online is exactly the same as this one. Moreover, this code, which I use to run on my own computer, has been in trouble. Most people who imitate this code copy it directly, and then send a blog, without any understanding of their own, even the comments are the same.
Code run unsuccessfully, tired ah, finally, let me google to this big guy's project, using his upload.c, cross-compiled, run on the machine, just started to report errors, and later found that the file saved on the server address to modify, after modification, test, success, TM is happy.
This file upload function has been bothering me for two or three days.
In my project: Network 3800, firmware openwrt (self-compiled), using the uhttpd server with op, server address / www. CGIC library is used, mainly including capture,cgic.c,cgic.h and makefile files, where the code in makefile is:
upload.cgi:cgic.h cgic.c mips-openwrt-linux-gcc -Wall upload.c cgic.c -o upload.cgi
mips-openwrt-linux-gcc is preceded by the Tab key, -Wall's W is capitalized, upload.c is the code of the big guy. My modification is in the File() function, where the big guy writes char name OnServer [1024]=". / upload/", I modified it to char name OnServer [1024]="/", that is, changed to the system root directory, and then the files I uploaded are in the root directory.
Thank you for writing a blog to let more people know.
To be convenient to others is to be convenient to oneself.
Finally, I enclose the address of the big man's project again. https://github.com/houwentaoff/cgic_file_up_down