• libcurl 上传文件至 web服务器


    测试环境搭建, 使用 wamp server (windows下的 apache+MySQL+php) 

    libcurl vc6 工程代码  下载地址:  http://download.csdn.net/detail/mtour/8060775

    处理上传文件 php  代码

    [php] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <?php  
    2.     
    3.   if ($_FILES["file"]["error"] > 0)  
    4.     {  
    5.     echo "Return Code: " . $_FILES["file"]["error"] . "<br />";  
    6.     }  
    7.   else  
    8.     {  
    9.     echo "Upload: " . $_FILES["file"]["name"] . "<br />";  
    10.     echo "Type: " . $_FILES["file"]["type"] . "<br />";  
    11.     echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";  
    12.     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";  
    13.   
    14.     if (file_exists("./" . $_FILES["file"]["name"]))  
    15.       {  
    16.       echo $_FILES["file"]["name"] . " already exists. ";  
    17.       }  
    18.     else  
    19.       {  
    20.       move_uploaded_file($_FILES["file"]["tmp_name"],  
    21.       "./" . $_FILES["file"]["name"]);  
    22.       echo "Stored in: " . "/" . $_FILES["file"]["name"];  
    23.       }  
    24.     }  
    25. ?>  


    libcurl  代码

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
      1. // libCur1.cpp : Defines the entry point for the console application.  
      2. //  
      3.   
      4. #include "stdafx.h"  
      5. #include <stdio.h>  
      6. #include "curl/curl.h"  
      7.   
      8. #define MAX_BUFF_LEN 1048576 /*1M*/  
      9. #define POST_URL "http://10.10.1.4/d/upload.php"  
      10.   
      11.   
      12. int get_file_size(char *filename)  
      13. {  
      14.     FILE* fp = NULL;  
      15.     int nFileLen = 0;  
      16.       
      17.     fp = fopen(filename, "rb");  
      18.       
      19.     if (fp == NULL)  
      20.     {  
      21.         return 0;  
      22.     }  
      23.       
      24.     fseek(fp,0,SEEK_END); //定位到文件末   
      25.     nFileLen = ftell(fp); //文件长度  
      26.     return nFileLen;  
      27. }  
      28.   
      29. int http_post_file(const char *url, const char *filename)  
      30. {  
      31.     CURL *curl = NULL;  
      32.        CURLcode res;  
      33.   
      34.       struct curl_httppost *post=NULL;  
      35.       struct curl_httppost *last=NULL;  
      36.       struct curl_slist *headerlist=NULL;  
      37.   
      38.     if(filename == NULL || url == NULL)  
      39.         return -1;  
      40.   
      41.     printf("URL: %s ", url);  
      42.     printf("filename: %s ", filename);  
      43.   
      44.     /* Add simple file section */  
      45.     if( curl_formadd(&post, &last, CURLFORM_COPYNAME, "file",  
      46.                CURLFORM_FILE, filename, CURLFORM_END) != 0)  
      47.     {  
      48.         fprintf(stderr, "curl_formadd error. ");  
      49.         return -1;  
      50.     }  
      51.       
      52.       /* Fill in the submit field too, even if this is rarely needed */  
      53.       curl_formadd(&post, &last,  
      54.                CURLFORM_COPYNAME, "submit",  
      55.                CURLFORM_COPYCONTENTS, "OK",  
      56.                CURLFORM_END);  
      57.   
      58.     //curl_global_init(CURL_GLOBAL_ALL);  
      59.     curl = curl_easy_init();  
      60.     if(curl == NULL)  
      61.     {  
      62.         fprintf(stderr, "curl_easy_init() error. ");  
      63.         curl_formfree(post);  
      64.         return -1;  
      65.     }  
      66.   
      67.     curl_easy_setopt(curl, CURLOPT_HEADER, 0);  
      68.     curl_easy_setopt(curl, CURLOPT_URL, url); /*Set URL*/  
      69.     curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);  
      70.     int timeout = 5;  
      71.     curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);  
      72.     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);  
      73.     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1);  
      74.   
      75.     res = curl_easy_perform(curl);  
      76.     if(res != CURLE_OK)  
      77.     {  
      78.         fprintf(stderr, "curl_easy_perform[%d] error. ", res);  
      79.         curl_formfree(post);  
      80.         return -1;  
      81.     }  
      82.   
      83.     curl_easy_cleanup(curl);      
      84.   
      85.     return 0;  
      86. }  
      87.   
      88. int main()  
      89. {     
      90.   
      91.     char sFilePath[128]="d:\20130828131421113.jpg";  
      92.   
      93.     //Check File Size  
      94.     if(get_file_size(sFilePath) >= MAX_BUFF_LEN)  
      95.     {  
      96.         fprintf(stderr, "File Size is Big! ");  
      97.         return -1;  
      98.     }  
      99.   
      100.     //POST File  
      101.     http_post_file(POST_URL, sFilePath);  
      102.   
      103.     getchar();  
      104.   
      105.     return 0;  
      106. }  
  • 相关阅读:
    COBBLER无人值守安装
    消息头 Content-Type引发的问题:Jmeter请求中postdata不是期望的,响应数据请求参数为null;已经请求没问题,可变量还是为空
    python爬虫-'gbk' codec can't encode character 'xa0' in position 134: illegal multibyte sequence
    正则表达式30分钟入门教程-链接
    linux常见命令学习汇总3-控制语句
    postman循环操作及响应判断-支持文本多变量输入
    linux常见命令学习汇总2-运算符
    linux常见命令学习汇总1
    Jmeter连接数据库方法与问题:Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
    mysql学习笔记-ifnull()函数与nullif()函数
  • 原文地址:https://www.cnblogs.com/lidabo/p/6405376.html
Copyright © 2020-2023  润新知