openwrt通过libcurl上传图片,服务器端通过PHP接收文件

一、客户端文件上传

  libcurl上传文件有两种方式:

  1、直接上传文件,类似form表单<input type=”file” />,<form enctype=”multipart/form-data”…;

  2、上传二进制流;

设定自定义头,都是使用一样的方法


struct curl_slist *headers=NULL;
headers = curl_slist_append(headers, "Content-Type: text/xml");
headers = curl_slist_append(headers, "Accept: text/html, */*;q=0.01");
//... //set headers
curl_easy_setopt(easyhandle, CURLOPT_HTTPHEADER, headers); //last free the header list
curl_slist_free_all(headers); /* free the header list */ 

附上开发中写的程序代码

 int UploadFile(char *file_name, char *dir_name, char *save_name)
{
char buff[];
CURL *curl; CURLM *multi_handle;
int still_running; struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
struct curl_slist *headerlist=NULL;
static const char buf[] = "Expect:"; sprintf(buff, "%s%s%s%s", dir_name, "-", save_name, ".jpg");
/* Fill in the file upload field. This makes libcurl load data from
the given file name when curl_easy_perform() is called. */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "file",
CURLFORM_FILE, file_name,
CURLFORM_CONTENTTYPE, "Image/jpeg",
CURLFORM_FILENAME, buff,
CURLFORM_END); /* Fill in the filename field */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "filename",
CURLFORM_COPYCONTENTS, save_name,
CURLFORM_END); /* Fill in the submit field too, even if this is rarely needed */
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "directory",
CURLFORM_COPYCONTENTS, dir_name,
CURLFORM_END); curl = curl_easy_init();
multi_handle = curl_multi_init(); /* initalize custom header list (stating that Expect: 100-continue is not
wanted */
headerlist = curl_slist_append(headerlist, buf);
if(curl && multi_handle) { /* what URL that receives this POST */
curl_easy_setopt(curl, CURLOPT_URL, "https://app.sopings.com/lock/control/_UploadFile.php");
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); if () {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, );
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, );
} curl_multi_add_handle(multi_handle, curl); curl_multi_perform(multi_handle, &still_running); do {
struct timeval timeout;
int rc; /* select() return code */ fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd = -; long curl_timeo = -; FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep); /* set a suitable timeout to play around with */
timeout.tv_sec = ;
timeout.tv_usec = ; curl_multi_timeout(multi_handle, &curl_timeo);
if(curl_timeo >= ) {
timeout.tv_sec = curl_timeo / ;
if(timeout.tv_sec > )
timeout.tv_sec = ;
else
timeout.tv_usec = (curl_timeo % ) * ;
} /* get file descriptors from the transfers */
curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); /* In a real-world program you OF COURSE check the return code of the
function calls. On success, the value of maxfd is guaranteed to be
greater or equal than -1. We call select(maxfd + 1, ...), specially in
case of (maxfd == -1), we call select(0, ...), which is basically equal
to sleep. */ rc = select(maxfd+, &fdread, &fdwrite, &fdexcep, &timeout); switch(rc) {
case -:
/* select error */
break;
case :
default:
/* timeout or readable/writable sockets */
printf("perform!\n");
curl_multi_perform(multi_handle, &still_running);
printf("running: %d!\n", still_running);
break;
}
} while(still_running); curl_multi_cleanup(multi_handle); /* always cleanup */
curl_easy_cleanup(curl); /* then cleanup the formpost chain */
curl_formfree(formpost); /* free slist */
curl_slist_free_all (headerlist);
}
return ;
}

服务器端的接收代码(PHP)

<?php

require_once (__DIR__."/../libs/Error.php");
include_once (__DIR__."/../db/Db.php");
/**
* 上传文件
* @author 戴栋根
* @version 1.0
* @created 16-三月-2016
*/
if ((($_FILES["file"]["type"] == "Image/png")
|| ($_FILES["file"]["type"] == "Image/jpeg")
|| ($_FILES["file"]["type"] == "Image/pjpeg"))
&& ($_FILES["file"]["size"] < 10000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo Error::getRetString(10021);
}
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($_FILES["directory"]["type"]))
{
mkdir ($_FILES["directory"]["type"]);
} if (file_exists($_FILES["directory"]["type"]."/" . $_FILES["filename"]["type"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$_FILES["directory"]["type"]."/" . $_FILES["filename"]["type"]);
echo "Stored in: " . $_FILES["directory"]["type"]."/" . $_FILES["filename"]["type"];
}
}
}
else
{
echo Error::getRetString(10007);
}
?>

libcurl开发文档链接

上一篇:Jquery 学习二


下一篇:转:Nginx RTMP 功能研究