I have been trying to upload an image to a local webserver based on php this is a simple php script that accepts the file parameter and saves it to the server
PHP Code:
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
This is my c++ code i have set all the required headers correctly and verified them on live http but when i run the console app it only shows me the form but does not upload the image please can anyone point me what is wrong
C++ Code:
LPSTR pszData = "WinHttpWriteData Example\r\n------------ThIs_Is_tHe_bouNdaRY_$--\r\n\r\n";
DWORD dwBytesWritten = 0;
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
hSession = WinHttpOpen(L"A WinHTTP Example Program/1.0",
WINHTTP_ACCESS_TYPE_NO_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
0);
if(hSession) {
hConnect = WinHttpConnect(hSession,
L"localhost",
INTERNET_DEFAULT_HTTP_PORT,
0);
}
if(hConnect) {
LPCTSTR types[] = { _T("multipart/form-data; boundary=---------------------------ThIs_Is_tHe_bouNdaRY_$"), L"image/jpeg" };
hRequest = WinHttpOpenRequest(hConnect,
L"POST", L"/locker/upload.php",
NULL, WINHTTP_NO_REFERER,
types, 0);
}
if(hRequest) {
bResults = WinHttpAddRequestHeaders(hRequest,
L"Host: localhost",
(ULONG)-1L,
WINHTTP_ADDREQ_FLAG_ADD);
bResults = WinHttpAddRequestHeaders(hRequest,
L"Connection: keep-alive",
(ULONG)-1L,
WINHTTP_ADDREQ_FLAG_ADD);
bResults = WinHttpAddRequestHeaders(hRequest,
L"Content-Dis-data; name=\"file\"; filename=\"blog.jpg\"",
(ULONG)-1L,
WINHTTP_ADDREQ_FLAG_ADD);
}
if(bResults) {
bResults = WinHttpSendRequest(hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0, WINHTTP_NO_REQUEST_DATA,
0, (DWORD)strlen(pszData), 0);
}
else
{
printf("Error %d open.\n", GetLastError());
}
if (bResults) {
bResults = WinHttpWriteData(hRequest, pszData,
(DWORD)strlen(pszData),
&dwBytesWritten);
}
else
{
printf("Error %d send.\n", GetLastError());
}
if(bResults) {
bResults = WinHttpReceiveResponse(hRequest, NULL);
}
else
{
printf("Error %d write.\n", GetLastError());
}
if (bResults)
{
do
{
dwSize = 0;
if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
{
printf("Error %u in WinHttpQueryDataAvailable.\n",
GetLastError());
break;
}
// No more available data.
if (!dwSize)
break;
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize+1];
if (!pszOutBuffer)
{
printf("Out of memory\n");
break;
}
// Read the Data.
ZeroMemory(pszOutBuffer, dwSize+1);
if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded))
{
printf( "Error %u in WinHttpReadData.\n", GetLastError());
}
else
{
printf("%s", pszOutBuffer);
}
delete [] pszOutBuffer;
if (!dwDownloaded)
break;
} while (dwSize > 0);
}
else
{
printf("Error %d resp.\n", GetLastError());
}
if(hRequest) WinHttpCloseHandle(hRequest);
if(hConnect) WinHttpCloseHandle(hConnect);
if(hSession) WinHttpCloseHandle(hSession);
getchar();