上传的文件与原来的文件不一样

时间:2013-08-22 10:52:32

标签: php c++

我有一个将文件上传到服务器的代码。出于测试目的,我使用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);
}

2 个答案:

答案 0 :(得分: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(似乎已关闭)。

答案 1 :(得分:-1)

当oly 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";
  }