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 */ 

 

附上开发中写的程序代码

  1 int UploadFile(char *file_name, char *dir_name, char *save_name)
  2 {
  3     char buff[128];
  4     CURL *curl;  
  5   
  6     CURLM *multi_handle;  
  7     int still_running;  
  8   
  9     struct curl_httppost *formpost=NULL;  
 10     struct curl_httppost *lastptr=NULL;  
 11     struct curl_slist *headerlist=NULL;  
 12     static const char buf[] = "Expect:";  
 13   
 14     sprintf(buff, "%s%s%s%s", dir_name, "-", save_name, ".jpg");
 15     /* Fill in the file upload field. This makes libcurl load data from 
 16         the given file name when curl_easy_perform() is called. */  
 17     curl_formadd(&formpost,  
 18                 &lastptr,  
 19                 CURLFORM_COPYNAME, "file",
 20                 CURLFORM_FILE, file_name,  
 21                 CURLFORM_CONTENTTYPE, "Image/jpeg",
 22                 CURLFORM_FILENAME, buff,
 23                 CURLFORM_END);  
 24   
 25     /* Fill in the filename field */  
 26     curl_formadd(&formpost,
 27                 &lastptr,
 28                 CURLFORM_COPYNAME, "filename",
 29                 CURLFORM_COPYCONTENTS, save_name,  
 30                 CURLFORM_END);  
 31   
 32     /* Fill in the submit field too, even if this is rarely needed */  
 33     curl_formadd(&formpost,  
 34                 &lastptr,  
 35                 CURLFORM_COPYNAME, "directory",  
 36                 CURLFORM_COPYCONTENTS, dir_name,  
 37                 CURLFORM_END);  
 38   
 39     curl = curl_easy_init();  
 40     multi_handle = curl_multi_init();  
 41   
 42     /* initalize custom header list (stating that Expect: 100-continue is not 
 43      wanted */  
 44     headerlist = curl_slist_append(headerlist, buf);  
 45     if(curl && multi_handle) {  
 46 
 47         /* what URL that receives this POST */  
 48         curl_easy_setopt(curl, CURLOPT_URL, "https://app.sopings.com/lock/control/_UploadFile.php");  
 49         curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);  
 50 
 51         curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);  
 52         curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);  
 53         
 54         if (1) {
 55             curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);  
 56             curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);  
 57         }            
 58 
 59         curl_multi_add_handle(multi_handle, curl);  
 60 
 61         curl_multi_perform(multi_handle, &still_running);  
 62 
 63         do {  
 64             struct timeval timeout;  
 65             int rc; /* select() return code */  
 66 
 67             fd_set fdread;  
 68             fd_set fdwrite;  
 69             fd_set fdexcep;  
 70             int maxfd = -1;  
 71 
 72             long curl_timeo = -1;  
 73 
 74             FD_ZERO(&fdread);  
 75             FD_ZERO(&fdwrite);  
 76             FD_ZERO(&fdexcep);  
 77 
 78             /* set a suitable timeout to play around with */  
 79             timeout.tv_sec = 1;  
 80             timeout.tv_usec = 0;  
 81 
 82             curl_multi_timeout(multi_handle, &curl_timeo);  
 83             if(curl_timeo >= 0) {  
 84                 timeout.tv_sec = curl_timeo / 1000;  
 85                 if(timeout.tv_sec > 1)  
 86                     timeout.tv_sec = 1;  
 87                 else  
 88                 timeout.tv_usec = (curl_timeo % 1000) * 1000;  
 89             }  
 90 
 91             /* get file descriptors from the transfers */  
 92             curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);  
 93 
 94             /* In a real-world program you OF COURSE check the return code of the 
 95              function calls.  On success, the value of maxfd is guaranteed to be 
 96              greater or equal than -1.  We call select(maxfd + 1, ...), specially in 
 97              case of (maxfd == -1), we call select(0, ...), which is basically equal 
 98              to sleep. */  
 99 
100             rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);  
101 
102             switch(rc) {  
103                 case -1:  
104                     /* select error */  
105                     break;  
106                 case 0:  
107                 default:  
108                     /* timeout or readable/writable sockets */  
109                     printf("perform!\n");  
110                     curl_multi_perform(multi_handle, &still_running);  
111                     printf("running: %d!\n", still_running);  
112                     break;  
113             }  
114         } while(still_running);  
115 
116         curl_multi_cleanup(multi_handle);  
117 
118         /* always cleanup */  
119         curl_easy_cleanup(curl);  
120 
121         /* then cleanup the formpost chain */  
122         curl_formfree(formpost);  
123 
124         /* free slist */  
125         curl_slist_free_all (headerlist);  
126     }  
127     return 0;  
128 }
View Code

 

服务器端的接收代码(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);
}
?>
View Code

 

 

libcurl开发文档链接

 

posted @ 2016-03-30 16:31  哪里来的木木  阅读(1546)  评论(0编辑  收藏  举报