上传的文件和原来的不一样
uploaded file is not same as the original was
我有一个代码,可以将文件上传到服务器上。出于测试目的,我使用Apache在我的PC上创建了一个本地服务器(localhost)。我的程序正在制作/上传与原始文件同名的文件,但其内容是复制的。在我的程序中,我使用了"file contents are here"这一行进行调试,这是上传文件中唯一一行。我使用了一个PHP脚本,它将接受文件并将其上传到指定的位置。现在我很困惑这是PHP的问题还是我的代码中的问题。这是我的代码片段:-
static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858rnContent-Disposition: form-data; name="uploadedfile"; filename="D:\er.txt"rnContent-Type: text/plainrnrnfile contents herern-----------------------------7d82751e2bc0858--rn";
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, 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);
}
阅读文档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对于代码示例(似乎已关闭)。
当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";
}
相关文章:
- 自制的上衣:看起来一样,但不完全相同
- 为什么将两个对象分配给另一个对象后,两个对象不一样?
- 为什么向量不支持 size 作为成员变量,就像 Java 类中的长度变量一样,而是函数 size()?
- 如何创建像标准库头一样的头,以便在不链接头中函数的所有对象文件的情况下编译程序?
- 2D数组值像数组一样传递,0不打印
- C++ x+=1 x++ 和 x = x +1 是不一样的
- 为什么重新执行我的多线程代码后输出不一样
- 发布的应用程序与原始应用程序不一样
- 上传的文件和原来的不一样
- 输入Std::cin与直接赋值Std::string变量不一样
- 为什么每次执行这个程序的CPU时间都不一样
- 高斯生成的核和书中给出的不一样.为什么?
- 为什么保存到文件和加载的时间(NULL)和仅仅时间(NULL)是不一样的
- &(*similarObject) 和 similarObject 之间的区别?它们不一样吗?
- DLL函数名与导出的不一样
- 多次调用同一对象的析构函数,其结果与标准中描述的不一样
- 位字段大小与预期不一样,原因是什么
- .pdb中的函数地址与.exe中的不一样,为什么?
- Mat和written image是不一样的
- dynamic_cast的行为与APP_STL:=gnustl_static不一样