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  代码

 

  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\n", url);  
  42.     printf("filename: %s\n", 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.\n");  
  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.\n");  
  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.\n", 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!\n");  
  97.         return -1;  
  98.     }  
  99.   
  100.     //POST File  
  101.     http_post_file(POST_URL, sFilePath);  
  102.   
  103.     getchar();  
  104.   
  105.     return 0;  
  106. }