i have a code that will upload a file on to a server. for testing purpose i create a local server(localhost) onto my PC by using Apache. my program is making/uploading file with the same name as of original file but its content is copied. in my program i used this line "file contents are here" for debugging purpose , and its the only line which is written in the uploaded file. i used a PHP script that will accept the file and upload it to the specified location. now i am confused that is it PHP's Problem or whether it is in my code. here is my code snipt :-
static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"D:\\er.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents here\r\n-----------------------------7d82751e2bc0858--\r\n";
static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858";
HINTERNET hSession = InternetOpen("MyBrowser",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(!hSession)
{
cout<<"Error: InternetOpen";
}
HINTERNET hConnect = InternetConnect(hSession, _T("localhost"),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if(!hConnect)
{
cout<<"Error: InternetConnect";
}
//HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T("upload.php"), NULL, NULL, (const char**)"*/*\0", 0, 1);
LPCTSTR rgpszAcceptTypes[] = {_T("*/*"), NULL};
HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",
_T("upload.php"), NULL, NULL,
rgpszAcceptTypes, 0, 1);
if(hRequest==NULL)
{
cout<<"Error: HttpOpenRequest";
}
BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
if(!sent)
{
cout<<"Error: HttpSendRequest "<<GetLastError();
}
char buffer[2048] = {};
DWORD bufferSize = sizeof(buffer);
BOOL success = HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &bufferSize, NULL);
if(!success)
{
std::cout<<"Error: HttpQueryInfo "<< GetLastError();
return 0;
}
std::cout << buffer << std::endl;
ZeroMemory(buffer, sizeof(buffer));
success = InternetReadFile(hRequest, buffer, sizeof(buffer), &bufferSize);
if(!success)
{
std::cout << "Error: InternetReadFile " << GetLastError();
return 0;
}
std::cout << buffer << std::endl;
//close any valid internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
getchar();
here is PHP script:-
if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) {
$uploadfile = $uploaddir . basename($_FILES['uploadedfile']['name']);
echo "File ". $_FILES['uploadedfile']['name'] ." uploaded successfully. ";
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully moved. ";
}
else
print_r($_FILES);
}
else {
echo "Upload Failed!!!";
print_r($_FILES);
}