uploadig adobe reader file in local host in c language
-
> I am uploading adobe files in local host it is uploading file successfully but it is showing corrupt or damage file after opening file please help me out to sort out this problem i will be thankful to you below is my entire code int main() { static char *filename = "tutorial.pdf"; //Filename to be loaded static char *filepath = "C:\\tutorial.pdf"; //Filename to be loaded `static char *type = "text/pdf";` static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"; static char boundary[] = "-----------------------------7d82751e2bc0858"; //Header boundary static char nameForm[] = "uploadedfile"; //Input form name static char iaddr[] = "localhost"; //IP address static char url[] = "/xampp/testing/upload.php?folder=aaaa&&foldername=bbbb"; char * buffer; //Buffer containing file + headers char * content; //Buffer containing file FILE * pFile; //File pointer long lSize; //File size size_t result; char *pos; // used in the loop // Open file pFile = fopen(filepath, "rb"); if (pFile == NULL) { printf("ERROR_OPEN_FILE"); getchar(); return ERROR_OPEN_FILE; } printf("OPEN_FILE\n"); // obtain file size: fseek(pFile, 0, SEEK_END); lSize = ftell(pFile); rewind(pFile); // allocate memory to contain the whole file: content = (char*)malloc(sizeof(char)*lSize); if (content == NULL) { printf("ERROR_MEMORY"); getchar(); return ERROR_OPEN_FILE; } printf("MEMORY_ALLOCATED\t \"%d\" \n", lSize); // copy the file into the buffer: result = fread(content, 1, lSize, pFile); rewind (pFile); if (result != lSize) { printf("ERROR_SIZE"); getchar(); return ERROR_OPEN_FILE; } printf("SIZE_OK\n"); // terminate fclose(pFile); printf("FILE_CLOSE\n"); //allocate memory to contain the whole file + HEADER buffer = (char*)malloc(sizeof(char)*lSize + 2048); //print header sprintf(buffer, "%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n", boundary, nameForm, filename); sprintf(buffer, "%sContent-Type: %s\r\n", buffer, type); sprintf(buffer, "%sContent-Length: %d\r\n", buffer, lSize); sprintf(buffer, "%s\r\n", buffer);
-
> I am uploading adobe files in local host it is uploading file successfully but it is showing corrupt or damage file after opening file please help me out to sort out this problem i will be thankful to you below is my entire code int main() { static char *filename = "tutorial.pdf"; //Filename to be loaded static char *filepath = "C:\\tutorial.pdf"; //Filename to be loaded `static char *type = "text/pdf";` static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"; static char boundary[] = "-----------------------------7d82751e2bc0858"; //Header boundary static char nameForm[] = "uploadedfile"; //Input form name static char iaddr[] = "localhost"; //IP address static char url[] = "/xampp/testing/upload.php?folder=aaaa&&foldername=bbbb"; char * buffer; //Buffer containing file + headers char * content; //Buffer containing file FILE * pFile; //File pointer long lSize; //File size size_t result; char *pos; // used in the loop // Open file pFile = fopen(filepath, "rb"); if (pFile == NULL) { printf("ERROR_OPEN_FILE"); getchar(); return ERROR_OPEN_FILE; } printf("OPEN_FILE\n"); // obtain file size: fseek(pFile, 0, SEEK_END); lSize = ftell(pFile); rewind(pFile); // allocate memory to contain the whole file: content = (char*)malloc(sizeof(char)*lSize); if (content == NULL) { printf("ERROR_MEMORY"); getchar(); return ERROR_OPEN_FILE; } printf("MEMORY_ALLOCATED\t \"%d\" \n", lSize); // copy the file into the buffer: result = fread(content, 1, lSize, pFile); rewind (pFile); if (result != lSize) { printf("ERROR_SIZE"); getchar(); return ERROR_OPEN_FILE; } printf("SIZE_OK\n"); // terminate fclose(pFile); printf("FILE_CLOSE\n"); //allocate memory to contain the whole file + HEADER buffer = (char*)malloc(sizeof(char)*lSize + 2048); //print header sprintf(buffer, "%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n", boundary, nameForm, filename); sprintf(buffer, "%sContent-Type: %s\r\n", buffer, type); sprintf(buffer, "%sContent-Length: %d\r\n", buffer, lSize); sprintf(buffer, "%s\r\n", buffer);
You should not pass the buffer itself as format argument to
sprintf
. It depends on the implementation (the used standard C library) if it is supported or not. But even if it is supported it is bad style. And in your case it is the reason for the corrupted buffer content here:sprintf(buffer, "%s\r\n", buffer);
memcpy(buffer + strlen(buffer),content,lSize);
sprintf(buffer, "%s\r\n", buffer);The PDF file is a binary file. While you use
memcpy
to append the binary data to your buffer, the nextsprintf
call stops at the first null byte truncating the buffer. A common solution is to use a buffer offset variable which is incremented by the return value ofsprintf
:int ofs = strlen(buffer);
ofs += sprintf(buffer + ofs, "\r\n");
memcpy(buffer + ofs,content,lSize);
ofs += lSize;
ofs += sprintf(buffer + ofs, "\r\n");