#include "base/HGDef.h" #include "base/HGInc.h" #include "base/HGUtility.h" #include #include "cgic.h" int cgiMain() { char mimiType[32] = "text/html; charset=utf-8"; char fileName[32] = "filename"; char remoteFileName[32] = "remote_filename"; cgiHeaderContentType(mimiType); char* scriptPath = getenv("PATH_INFO"); fprintf(cgiOut, "scriptPath: %s
\n", scriptPath); char* scriptFullPath = getenv("PATH_TRANSLATED"); fprintf(cgiOut, "scriptFullPath: %s
\n", scriptFullPath); if (NULL == scriptPath || NULL == scriptFullPath) { fprintf(cgiOut, "

get PATH_INFO or PATH_TRANSLATED failed.

\n"); return cgiFormNotFound; } char stdScriptPath[256]; HGBase_StandardiseFileName(scriptPath, stdScriptPath, 256); char* p = strstr(scriptFullPath, stdScriptPath); if (NULL == p) { fprintf(cgiOut, "

PATH_INFO or PATH_TRANSLATED error.

\n"); return cgiFormNotFound; } std::string rootPath(scriptFullPath, p - scriptFullPath); fprintf(cgiOut, "rootPath: "); cgiHtmlEscape(rootPath.c_str()); fprintf(cgiOut, "
\n"); char name[512]; if (cgiFormFileName(fileName, name, sizeof(name)) != cgiFormSuccess) { fprintf(cgiOut, "

no filename.

\n"); return cgiFormNoFileName; } fprintf(cgiOut, "filename: "); cgiHtmlEscape(name); fprintf(cgiOut, "
\n"); int size = 0; cgiFormFileSize(fileName, &size); if (size <= 0) { fprintf(cgiOut, "

bad file size.

\n"); return cgiFormEmpty; } fprintf(cgiOut, "file size: %d bytes
\n", size); char remoteFilePath[512]; if (cgiFormString(remoteFileName, remoteFilePath, sizeof(remoteFilePath)) != cgiFormSuccess) { fprintf(cgiOut, "

no remote filename.

\n"); return cgiFormNoFileName; } fprintf(cgiOut, "remote_filepath: "); cgiHtmlEscape(remoteFilePath); fprintf(cgiOut, "
\n"); cgiFilePtr file = NULL; if (cgiFormFileOpen(fileName, &file) != cgiFormSuccess) { fprintf(cgiOut, "

Could not open the file.

\n"); return cgiFormOpenFailed; } std::string savePath = rootPath + remoteFilePath; char stdSavePath[256]; HGBase_StandardiseFileName(savePath.c_str(), stdSavePath, 256); char stdSaveDir[256]; HGBase_GetFilePath(stdSavePath, stdSaveDir, 256); HGBase_CreateDir(stdSaveDir); fprintf(cgiOut, "savePath: "); cgiHtmlEscape(stdSavePath); fprintf(cgiOut, "
\n"); FILE *fd = fopen(stdSavePath, "wb+"); if (fd == NULL) { fprintf(cgiOut, "

create file failed.

\n"); cgiFormFileClose(file); return cgiFormOpenFailed; } char tmp[1024]; int got = 0; while (cgiFormFileRead(file, tmp, 1024, &got) == cgiFormSuccess) { fwrite(tmp, got, sizeof(char), fd); //把读出的content写入新文件 } fclose(fd); cgiFormFileClose(file); //打印输出 fprintf(cgiOut, "

upload file success.

\n"); return cgiFormSuccess; }