0

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);
}
Jaak Kütt
  • 2,566
  • 4
  • 31
  • 39
user2682006
  • 13
  • 2
  • 7
  • Did not get one thing: For what is C++ code used? If you set apache webserver with php, why do you need to use then C++ code? – Wiggler Jtag Aug 22 '13 at 10:57
  • The PHP code looks fine. – Michael Berkowski Aug 22 '13 at 11:00
  • Yes php code looks fine, look into Related there are same scripts as you've used, maybe there someone already solved the problem. + What do you mean not the same as original was? Bytes are not the same or? Maybe you should use some packet sniffer to see where exactly could be the problem. – Wiggler Jtag Aug 22 '13 at 11:04
  • http://stackoverflow.com/questions/18307919/upload-file-using-http-getting-error-httpsendreuest-12005 – Wiggler Jtag Aug 22 '13 at 11:10
  • i used apache server just to test my program. now i am using a host server where i can save my files. – user2682006 Aug 22 '13 at 11:23
  • to better explain my problem, suppose i have a text file in which i wrote about myself. now if i used this file in my program to upload, then the uploaded file actually contains this line only "line contents are here" not the lines which i wrote – user2682006 Aug 22 '13 at 11:25

2 Answers2

0

Reading the documentation http://msdn.microsoft.com/en-us/library/windows/desktop/aa385103%28v=vs.85%29.aspx, you need to call InternetReadFile in a loop as it might only return one line: "Also, converted lines might not completely fill the buffer, so InternetReadFile can return with less data in lpBuffer than requested. Subsequent reads will retrieve all the converted HTML. The application must again check that all data is retrieved as described previously." See http://support.microsoft.com/kb/149413 for a code example (appears to be down).

Adder
  • 5,708
  • 1
  • 28
  • 56
-1

When oly php code can upload a file then why to use extra c++ code. the following code will upload the file to upload folder with their original contents:

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
Navya
  • 657
  • 2
  • 10
  • 20