CURL和LIBCURLC++代码上传本地⽂件
解决了什么问题:curl在使⽤各种⽅式上传⽂件到服务器。
⼀般的⽂件上传是通过html表单进⾏的,通过可以不经过浏览器,直接在服务器端模拟进⾏表单提交,完成POST数据、⽂件上传等功能。
服务器端PHP代码:
<?php
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 200000)) { if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
} else {
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo$_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "data/" . $_FILES["file"]["name"]);
// echo "Stored in: " . "data/" . $_FILES["file"]["name"];
echo "⽂件上传成功!";
}
}
} else {
echo " ⽂件⽆效!";
}
?>
客户端提交的form:
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" class="btn btn-primary btn-large" value="上传⽂件"/>
</form>
在控制台使⽤curl上传的例⼦:
curl -F "action=upload.php" -F "method=post" -F "file=@/Users/mark/xx.gif" ylg.zs108:8080/tmp/upload.php
使⽤libcurl实现的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#define POSTURL "ylg.zs108:8080/tmp/upload.php"
#define POSTFIELDS "?action=upload&method=post"
#define FILENAME "curlposttest.log"
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);
int main(int argc, char *argv[]) {
CURL *curl;
CURLcode res;
FILE *fptr;
struct curl_slist *http_header = NULL;
if ((fptr = fopen(FILENAME, "w")) == NULL) {
fprintf(stderr, "fopen file error: %s\n", FILENAME);
exit(1);
}
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr);
struct curl_httppost *formpost = 0;
struct curl_httppost *lastptr = 0;
curl_formadd(&formpost, &lastptr, CURLFORM_PTRNAME, "reqformat", CURLFORM_PTRCONTENTS, "plain", CURLFORM_END);
curl_formadd(&formpost, &lastptr, CURLFORM_PTRNAME, "file", CURLFORM_FILE, "/Users/hanyanyan/xx.gif", CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_URL, POSTURL);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
res = curl_easy_perform(curl);
}
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
FILE *fptr = (FILE*)userp;
fwrite(buffer, size, nmemb, fptr);
}
编译⽅式:gcc -o curl_post_file filename.c -lcurl
后期调整:
<!DOCTYPE html>
<!-- saved from url=(0044)ylg.zs108:8080/dp/uploadtest.html -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css"></style></head>
<body>
<div class="containter-fluid">
<div class="row-fluid">
<div class="span8 offset2">
<form action="ylg.zs108:8080/dp/upload.php" method="post" enctype="multipart/form-data">
<table cellpadding="10" border="1">
<tbody><tr>
<td>wid</td>
<td><input type="text" name="wid" value="dengpan"></td>
</tr>
<tr>
<td>text</td>
<td><input type="text" name="text" value="哈哈哈呵呵"></td>
</tr>
<tr>
<td>stime</td>
<td><input type="text" name="stime" value="13"></td>
</tr>
<tr>
<td>ssize</td>
<td><input type="text" name="ssize" value="15"></td>
</tr>
curl是什么命令<tr>
<td>lid</td>
<td><input type="text" name="lid" value="2"></td>
</tr>
<tr>
<td>type</td>
<td><input type="text" name="type" value="2"></td>
</tr>
<tr>
<td>sound</td>
<td><input type="file" name="sound"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交"></td>
</tr>
</tbody></table>
</form>
</div>
</div>
</div>
</body></html>
命令⾏:
curl -F "method=post" -F "action=upload.php" -F "wid=dengpan" -F "text=hh" -F "stime=13" -F "ssize=15" -F "lid=2" -F "type=2" -F "sound=@/Users/hanyanyan /million.amr" ylg.zs108:8080/dp/upload.php
返回代码:
{"code":1,"result":{"sound":"http:\/\/ylg.zs108:8080\/dp\/data\/sounds\/1378815106-a3864fc57553bf4b299b4d4c0ce07649.amr"}}
最后在贴⼀段PHP上传的代码吧
需要被上传的⽂件需要在⽂件名前加上“@”以⽰区分,并且,⽂件名需要是完整路径。
以下php函数来模拟html表单的提交数据:
function uploadByCURL($post_data,$post_url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $post_url);
curl_setopt($curl, CURLOPT_POST, 1 );
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_USERAGENT,"Mozilla/4.0");
$error = curl_error($curl);
return$error ? $error : $result;
}
/*函数的使⽤:*/
$url = "127.0.0.1/app.php"; $data = array(
"username" => $username,
"password" => $password,
"file1" => "@".realpath("photo1.jpg"), "file2" => "@".realpath("l") );
print_r(uploadByCURL($data,$url));
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论