ZIP文件没有完全从c++上传到PHP
ZIP files not being completely uploaded from C++ to PHP
我有一个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
相关文章:
- 将加密消息从 php 发送到 C++ 应用程序,然后使用 CryptoPP 进行解密
- PHP Profiler语言 - 使用 C++ 从 PHP 扩展获取客户端 IP 和 URL
- Nginx RTMP与InstagramLive-PHP不起作用
- 如何使用PHP将上传到本地主机的一个html文件重定向到磁盘上的另一个html文档
- PHP & C++ $_FILES 和 HttpSendRequestA
- 如何使用 PHP-CPP 在 PHP 类中添加静态数组
- 如何在PHPCPP库中将C++map<string,map<string>>转换为php::value?
- 为什么用C++编写的 phpcpp 扩展函数比用 PHP 编写的函数慢
- QT:AES-256-CBC 根据 PHP 代码在C++中加密/解密
- 从 PHP 调用已编译的C++可执行文件来创建套接字
- 如何在 ubuntu 上的 php 脚本中获取程序(c,c++,java,python,php)的执行时间和内存使用量?
- 文件写入在PHP扩展c / c ++(linux)中不起作用
- 无论如何可以使用 C++/C 甚至 python 和 php
- 当 PHP、C++ 和 shell 脚本尝试访问同一个文件时,如何避免错误?
- PHP 中的实时仪表板,可从C++共享数据
- PHP / C++:共享内存时出现shm_open()错误
- 在PHP扩展中使用emalloc从线程时出现隔离错误
- 生成可以在PHP中加密的密钥,该密钥可以直接放置在C 代码中以进行解密
- 如何将JSON发送到PHP脚本并使用QT获得答案
- 当我使用 system() 在 PHP 中运行命令时没有LD_LIBRARY_PATH