ZIP文件没有完全从c++上传到PHP

ZIP files not being completely uploaded from C++ to PHP

本文关键字:c++ PHP 文件 ZIP      更新时间:2023-10-16

我有一个c++控制台应用程序,它将zip归档文件上传到php页面。上传是工作的文本和图像文件,但不工作的zip文件,我需要。

我使用这里给出的代码:发送文件POST c++

我修改了c++代码,最终代码如下。

#include <windows.h>
#include <wininet.h>
#include <iostream>
#include <tchar.h>
#pragma comment(lib,"wininet.lib")
#define ERROR_OPEN_FILE       10
#define ERROR_MEMORY          11
#define ERROR_SIZE            12
#define ERROR_INTERNET_OPEN   13
#define ERROR_INTERNET_CONN   14
#define ERROR_INTERNET_REQ    15
#define ERROR_INTERNET_SEND   16
using namespace std;
int main()
{
 // Local variables
 static char *filename   = "hello.zip";   //Filename to be loaded
 static char *filepath   = "C:\Users\username\Desktop\hello.zip";   //Filename to be loaded
 static char *type       = "application/zip";
 static char boundary[]  = "--BOUNDARY---";            //Header boundary
 static char nameForm[]  = "file";     //Input form name
 static char iaddr[]     = "localhost";        //IP address
 static char url[]       = "/u.php";         //URL
 char hdrs[512]={'-'};                  //Headers
 char * buffer;                   //Buffer containing file + headers
 char * content;                  //Buffer containing file
 FILE * pFile;                    //File pointer
 long lSize;                      //File size
 size_t result;                   
 // Open file
 pFile = fopen ( filepath , "rb" );
 if (pFile==NULL) 
 {
     printf("ERROR_OPEN_FILE");
     getchar();
     return ERROR_OPEN_FILE;
 }
 printf("OPEN_FILEn");
 // 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+1));
 if (content == NULL) 
 {
     printf("ERROR_MEMORY");
     getchar();
     return ERROR_OPEN_FILE;
 }
 printf("MEMORY_ALLOCATEDt "%d" n",&lSize);
 // copy the file into the buffer:
 result = fread (content,1,lSize,pFile);
 if (result != lSize) 
 {
     printf("ERROR_SIZE");
     getchar();
     return ERROR_OPEN_FILE;
 }
 printf("SIZE_OKn");
 content[lSize] = '';
 // terminate
 fclose (pFile);
 printf("FILE_CLOSEn");
 //allocate memory to contain the whole file + HEADER
 buffer = (char*) malloc (sizeof(char)*lSize + 2048);
 //print header
 sprintf(hdrs,"Content-Type: multipart/form-data; boundary=%s",boundary);
 sprintf(buffer,"%srnContent-Disposition: form-data; name="%s"; filename="%s"rn",boundary,nameForm,filename);
 sprintf(buffer,"%sContent-Type: %srn",buffer,type);
 sprintf(buffer,"%srn%s",buffer,content);
 sprintf(buffer,"%srn--%s--rn",buffer,boundary);
 printf("%s", buffer);
 //Open internet connection
 HINTERNET hSession = InternetOpen("WINDOWS",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
 if(hSession==NULL) 
 {
     printf("ERROR_INTERNET_OPEN");
     getchar();
     return ERROR_OPEN_FILE;
 }
 printf("INTERNET_OPENEDn");
 HINTERNET hConnect = InternetConnect(hSession, iaddr,INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
 if(hConnect==NULL) 
 {
     printf("ERROR_INTERNET_CONN");
     getchar();
     return ERROR_INTERNET_CONN;
 }
 printf("INTERNET_CONNECTEDn");
 HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T(url),NULL, NULL, NULL,INTERNET_FLAG_RELOAD, 1);
 if(hRequest==NULL) 
  {
     printf("ERROR_INTERNET_REQ");
     getchar();
 }
 printf("INTERNET_REQ_OPENn");
 BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));
 if(!sent) 
 {
     printf("ERROR_INTERNET_SEND");
     getchar();
     return ERROR_INTERNET_CONN;
 }
 printf("INTERNET_SEND_OKn");
 InternetCloseHandle(hSession);
 InternetCloseHandle(hConnect);
 InternetCloseHandle(hRequest);
 getchar();
 return 0;
}

c++返回如下:

OPEN_FILE
MEMORY_ALLOCATED         "2348964"
SIZE_OK
FILE_CLOSE
----BOUNDARY--
Content-Disposition: form-data; name="image"; filename="hello.zip"
Content-Type: application/zip
PK♥♦¶
----BOUNDARY--
INTERNET_OPENED
INTERNET_CONNECTED
INTERNET_REQ_OPEN
INTERNET_SEND_OK

似乎zip文件没有被完全读取,因为只显示前5个字符(PK♥♦¶)而不是整个文件。我试过增加hdrs的大小来容纳整个内容,但不起作用。zip文件的大小约为37KB。请帮忙,因为我不太熟悉c++中的http请求。

EDIT:终于得到了它的工作后,我遇到了一个类似的问题:如何在c++中使用wininet发送一个zip文件

你的问题根本与http请求无关。

基本上你问的是:为什么我不能处理二进制数据作为字符串?

答案是:因为字符串函数(如printf, strlen)都在遇到第一个空字符时停止解析,而二进制文件(如zip文件)确实包含空字符。

所以当发送数据时,不要试图以字符串的形式获取数据的长度:BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));

请使用lSize

中已有的长度