如题,我用c 来实现post 上传文件,但是一直都是失败的,找不出什么原因,我用curl命令上传也是成功的。不知道错在什么地方。请帮忙看一下。服务器是架在本地的。我写个简单的php测试页面,是成功的。
如下:curl -F "action=upload" -F "[email protected]" http://localhost/updatefile.php我的c代码如下:
int main(int args ,char **argv)
{
CURLcode res;
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
const char * localfile = "/home/gary/share/c++/curl_test/a.txt";
curl_global_init(CURL_GLOBAL_ALL);
if(0!=curl_formadd(&post, &last, CURLFORM_COPYNAME, "userfile", CURLFORM_FILECONTENT, "a.txt", CURLFORM_END))
{
printf("curl_formadd error!\n");
return 0;
}
curl_formadd(&post, &last, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Send", CURLFORM_END);
CURL *easy_handle = curl_easy_init();
if(NULL == easy_handle)
{
printf("get a easy handle failed! \N");
// fclose(fp);
curl_global_cleanup();
return -1;
}
FILE * fp = fopen(localfile, "rb");
if(fp==NULL)
{
printf("can't open file \n");
curl_global_cleanup();
return -1;
} fseek(fp,0,2);
int file_size = ftell(fp);
rewind(fp);
fclose(fp);
curl_easy_setopt(easy_handle,CURLOPT_VERBOSE,1);
curl_easy_setopt(easy_handle, CURLOPT_INFILESIZE_LARGE, file_size); // upload file size--- Content-Length: size
curl_easy_setopt(easy_handle, CURLOPT_HEADER, 1);
curl_easy_setopt(easy_handle, CURLOPT_URL, "http://localhost/updatefile.php");
curl_easy_setopt(easy_handle, CURLOPT_POST, 1L);
curl_easy_setopt(easy_handle, CURLOPT_HTTPPOST, post);
res=curl_easy_perform(easy_handle);
if(res != 0)
{
printf("err string %s", curl_easy_strerror(res));
}
curl_formfree(post);
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
return 0;
} 我的php代码如下:updatefile.php
<?php$uploadfile = '/var/www/upload/'.$_FILES['userfile']['name'];//uploadΪÉÏ´«µ½·þÎñ»úÆ÷µÄÎļþ¼Ðecho $uploadfile;if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{
echo "OK!";
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile))
{
print "File is valid, and was successfully uploaded. Here's some more debugging info:\n";
print_r($_FILES);
}else
{
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}我的html页为updatefile.htm
<html>
<head>
<title>ÉÏÔØÎļþ±íµ¥</title>
</head>
<body>
<form ENCTYPE="multipart/form-data" ACTION="updatefile.php" METHOD="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<div align="center"><center> ÇëÑ¡È¡Îļþ:
<input NAME="userfile" TYPE="file">
<input name="submit" TYPE="submit" VALUE="Send">
</center></div>
</form>
</body>
</html>
c代码生成可执行程序,运行后结果如下:
* About to connect() to localhost port 80 (#0)
* Trying 127.0.0.1... * connected
> POST /updatefile.php HTTP/1.1
Host: localhost
Accept: */*
Content-Length: 246
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------b8bfb4829fb6< HTTP/1.1 100 Continue
HTTP/1.1 100 Continue< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Mon, 15 Apr 2013 09:38:29 GMT
Date: Mon, 15 Apr 2013 09:38:29 GMT
< Server: Apache/2.2.22 (Ubuntu)
Server: Apache/2.2.22 (Ubuntu)
< X-Powered-By: PHP/5.3.10-1ubuntu3.6
X-Powered-By: PHP/5.3.10-1ubuntu3.6
< Vary: Accept-Encoding
Vary: Accept-Encoding
< Content-Length: 452
Content-Length: 452
< Content-Type: text/html
Content-Type: text/html< Notice: Undefined index: userfile in /var/www/updatefile.php on line 3
/var/www/upload/
Notice: Undefined index: userfile in /var/www/updatefile.php on line 7Notice: Undefined index: userfile in /var/www/updatefile.php on line 12Notice: Undefined variable: fileextname in /var/www/updatefile.php on line 45Notice: Undefined index: userfile in /var/www/updatefile.php on line 47
Possible file upload attack! Here's some debugging info:
Array
(
)
* Connection #0 to host localhost left intact
* Closing connection #0到web服务器相应的目录找不到上传的文件。用curl命令执行后结果如下: curl -F "action=upload" -F "[email protected]" http://localhost/updatefile.php
/var/www/upload/a.txtOK!txtFile is valid, and was successfully uploaded. Here's some more debugging info:
Array
(
[userfile] => Array
(
[name] => a.txt
[type] => text/plain
[tmp_name] => /tmp/phpnSCiqq
[error] => 0
[size] => 4
))
在web服务器上对应的目录可以找到上传的文件。
如下:curl -F "action=upload" -F "[email protected]" http://localhost/updatefile.php我的c代码如下:
int main(int args ,char **argv)
{
CURLcode res;
struct curl_httppost *post = NULL;
struct curl_httppost *last = NULL;
const char * localfile = "/home/gary/share/c++/curl_test/a.txt";
curl_global_init(CURL_GLOBAL_ALL);
if(0!=curl_formadd(&post, &last, CURLFORM_COPYNAME, "userfile", CURLFORM_FILECONTENT, "a.txt", CURLFORM_END))
{
printf("curl_formadd error!\n");
return 0;
}
curl_formadd(&post, &last, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "Send", CURLFORM_END);
CURL *easy_handle = curl_easy_init();
if(NULL == easy_handle)
{
printf("get a easy handle failed! \N");
// fclose(fp);
curl_global_cleanup();
return -1;
}
FILE * fp = fopen(localfile, "rb");
if(fp==NULL)
{
printf("can't open file \n");
curl_global_cleanup();
return -1;
} fseek(fp,0,2);
int file_size = ftell(fp);
rewind(fp);
fclose(fp);
curl_easy_setopt(easy_handle,CURLOPT_VERBOSE,1);
curl_easy_setopt(easy_handle, CURLOPT_INFILESIZE_LARGE, file_size); // upload file size--- Content-Length: size
curl_easy_setopt(easy_handle, CURLOPT_HEADER, 1);
curl_easy_setopt(easy_handle, CURLOPT_URL, "http://localhost/updatefile.php");
curl_easy_setopt(easy_handle, CURLOPT_POST, 1L);
curl_easy_setopt(easy_handle, CURLOPT_HTTPPOST, post);
res=curl_easy_perform(easy_handle);
if(res != 0)
{
printf("err string %s", curl_easy_strerror(res));
}
curl_formfree(post);
curl_easy_cleanup(easy_handle);
curl_global_cleanup();
return 0;
} 我的php代码如下:updatefile.php
<?php$uploadfile = '/var/www/upload/'.$_FILES['userfile']['name'];//uploadΪÉÏ´«µ½·þÎñ»úÆ÷µÄÎļþ¼Ðecho $uploadfile;if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{
echo "OK!";
}
if (move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile))
{
print "File is valid, and was successfully uploaded. Here's some more debugging info:\n";
print_r($_FILES);
}else
{
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}我的html页为updatefile.htm
<html>
<head>
<title>ÉÏÔØÎļþ±íµ¥</title>
</head>
<body>
<form ENCTYPE="multipart/form-data" ACTION="updatefile.php" METHOD="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<div align="center"><center> ÇëÑ¡È¡Îļþ:
<input NAME="userfile" TYPE="file">
<input name="submit" TYPE="submit" VALUE="Send">
</center></div>
</form>
</body>
</html>
c代码生成可执行程序,运行后结果如下:
* About to connect() to localhost port 80 (#0)
* Trying 127.0.0.1... * connected
> POST /updatefile.php HTTP/1.1
Host: localhost
Accept: */*
Content-Length: 246
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------b8bfb4829fb6< HTTP/1.1 100 Continue
HTTP/1.1 100 Continue< HTTP/1.1 200 OK
HTTP/1.1 200 OK
< Date: Mon, 15 Apr 2013 09:38:29 GMT
Date: Mon, 15 Apr 2013 09:38:29 GMT
< Server: Apache/2.2.22 (Ubuntu)
Server: Apache/2.2.22 (Ubuntu)
< X-Powered-By: PHP/5.3.10-1ubuntu3.6
X-Powered-By: PHP/5.3.10-1ubuntu3.6
< Vary: Accept-Encoding
Vary: Accept-Encoding
< Content-Length: 452
Content-Length: 452
< Content-Type: text/html
Content-Type: text/html< Notice: Undefined index: userfile in /var/www/updatefile.php on line 3
/var/www/upload/
Notice: Undefined index: userfile in /var/www/updatefile.php on line 7Notice: Undefined index: userfile in /var/www/updatefile.php on line 12Notice: Undefined variable: fileextname in /var/www/updatefile.php on line 45Notice: Undefined index: userfile in /var/www/updatefile.php on line 47
Possible file upload attack! Here's some debugging info:
Array
(
)
* Connection #0 to host localhost left intact
* Closing connection #0到web服务器相应的目录找不到上传的文件。用curl命令执行后结果如下: curl -F "action=upload" -F "[email protected]" http://localhost/updatefile.php
/var/www/upload/a.txtOK!txtFile is valid, and was successfully uploaded. Here's some more debugging info:
Array
(
[userfile] => Array
(
[name] => a.txt
[type] => text/plain
[tmp_name] => /tmp/phpnSCiqq
[error] => 0
[size] => 4
))
在web服务器上对应的目录可以找到上传的文件。
看看结果
Array
(
)我的c代码中有print_r($_FILES);这样的打印语句。但执行linux命令curl ,在接受页updatefile.php,打印的语句为如下:,问题在于我不清楚为什么c写的程序打印print_r($_FILES),没有结果。
Array
(
[userfile] => Array
(
[name] => a.txt
[type] => text/plain
[tmp_name] => /tmp/phpnSCiqq
[error] => 0
[size] => 4
))
抓一包curl的包,与你的C程序包,对比一下,差异在哪,应可找到问下题。
Array是空的,当然是空的,
因为输出文件为 /var/www/upload/ 正确的是 /var/www/upload/a.txt
说明没有得到a.txt 所以,在3 7 12行有错,就报对了。$_FILES 是PHP变量,且与浏览器对话有关。你的浏览器对话根本没有,所以$_FILES 这个值是空的。 知道怎么改了吧。