0

我有一个将文件上传到服务器的代码。出于测试目的,我使用 Apache 在我的 PC 上创建了一个本地服务器(localhost)。我的程序正在制作/上传与原始文件同名的文件,但其内容已被复制。在我的程序中,我使用“文件内容在这里”这一行进行调试,它是写入上传文件的唯一行。我使用了一个 PHP 脚本,它将接受文件并将其上传到指定位置。现在我很困惑这是PHP的问题还是它是否在我的代码中。这是我的代码片段:-

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();

这是 PHP 脚本:-

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);
}
4

2 回答 2

0

阅读文档http://msdn.microsoft.com/en-us/library/windows/desktop/aa385103%28v=vs.85%29.aspx,您需要循环调用 InternetReadFile 因为它可能只返回一行:“此外,转换后的行可能不会完全填满缓冲区,因此 InternetReadFile 可以返回 lpBuffer 中的数据少于请求的数据。后续读取将检索所有转换后的 HTML。应用程序必须再次检查所有数据是否如前所述检索。“ 有关代码示例(似乎已关闭),请参见http://support.microsoft.com/kb/149413 。

于 2013-08-22T11:48:23.100 回答
-1

当只有 php 代码可以上传文件时,为什么要使用额外的 c++ 代码。以下代码会将文件上传到上传文件夹及其原始内容:

$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";
  }
于 2013-08-22T11:01:34.673 回答