最近刚写完一个cocos2dx,从客户端上传到php 服务器 的 简单 测试程序,代码可以复制直接运行 在阅读代码前最好可以先花点时间了解一点php 和libcurl的相关知识,并不会花费太长时间,由于我做的时候对php不是特别熟悉,所有使用libcurl拼凑表单的时候出现一
最近刚写完一个cocos2dx,从客户端上传到php服务器的简单测试程序,代码可以复制直接运行
在阅读代码前最好可以先花点时间了解一点php 和libcurl的相关知识,并不会花费太长时间,由于我做的时候对php不是特别熟悉,所有使用libcurl拼凑表单的时候出现一个问题,花了我一阵时间去解决。下方代码只是简单的demo,代码有些粗糙,但对于大家的理解应该影响不大
我使用XAMMP中的Apache服务器,php脚本放在\xampp\htdocs\目录下,并在当前目录创建upload文件夹用来存放上传的文件
1:
ReceiveFile.php
<?php /* 设定上传目录 */ $dest_dir='./upload'; /* 检测上传目录是否存在 */ if( !is_dir($dest_dir) || !is_writeable($dest_dir) ) { die("上传目录 ".$dest_dir." 不存在或无法写入"); } /* 设置允许上传<strong>文件的类型 */ $type=array("rar","zip","txt","c","JPG"); /* 获取上传<strong>文件</strong>信息 */ // $upfile=&$HTTP_POST_FILES['file']; $upfile=$_FILES["file"]; 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>"; /* 获取<strong>文件</strong>后缀名函数 */ function fileext($filename) { return substr(strrchr($filename, '.'), 1); } //echo "Temp file: " . fileext($upfile['name'] . "<br>"; /* 判断上传<strong>文件</strong>类型 */ if( !in_array( strtolower( fileext($upfile['name'] ) ),$type) ) { $text=implode(",",$type); echo "对不起,您只能上传以下类型<strong>文件</strong>: ",$text,"<br>"; } else { /* 设置<strong>文件</strong>名为"日期_<strong>文件</strong>名" */ $dest=$dest_dir.'/'.date("ymdHis")."_".$upfile['name']; /* 移动上传<strong>文件</strong>到指定<strong>文件</strong>夹 */ $state=move_uploaded_file($upfile['tmp_name'],$dest); if ($state) { print("<strong>文件</strong>上传成功!<br>"); print("<strong>文件</strong>名:".$dest."<br>"); print("上传的<strong>文件</strong>大小:".( round($upfile['size'] / 1024,2) )." KB<br>"); } else { /* 处理错误信息 */ switch($upfile['error']) { case 1 : die("上传<strong>文件</strong>大小超出 php.ini:upload_max_filesize 限制<br>"); case 2 : die("上传<strong>文件</strong>大小超出 MAX_FILE_SIZE 限制<br>"); case 3 : die("<strong>文件</strong>仅被部分上传<br>"); case 4 : die("没有<strong>文件</strong>被上传<br>"); case 5 : die("找不到临时<strong>文件</strong>夹<br>"); case 6 : die("<strong>文件</strong>写入失败<br>"); } } } ?>
upload.html 表单:
则会出现页面,选择相应的文件上传则会提示
注:php页面会有上传大小超过1M的限制
3:
接下来就要最重要的部分了,使用libcurl库来实现upload.html表单,实现上传功能
Httpupload.h
#ifndef COMMON_LINUX_HTTP_UPLOAD_H__ #define COMMON_LINUX_HTTP_UPLOAD_H__ #include <map> #include <string> using std::map; class HTTPUpload { <span><br> Httpupload.cpp<br> <pre class="brush:php;toolbar:false">#include <assert.h> #include <curl> #include <curl> #include <stdio.h> #include <vector> #include "stdafx.h" #include "HttpUpload.h" using namespace cocos2d; using namespace std; // Callback to get the response data from server. static size_t WriteCallback(void *ptr, size_t size, size_t nmemb, void *userp) { if (!userp) return 0; string *response = reinterpret_cast<string>(userp); size_t real_size = size * nmemb; response->append(reinterpret_cast<char>(ptr), real_size); std::cout ¶meters, const string &upload_file, //"E:\\install.res.1041.zip" const string &file_part_name, //"upload_file_minidump", const string &proxy, const string &proxy_user_pwd, const string &ca_certificate_file, string *response_body, long *response_code, string *error_description) { if (response_code != NULL) *response_code = 0; if (!CheckParameters(parameters)) return false; curl_global_init(CURL_GLOBAL_ALL); CURL *curl = curl_easy_init(); if (error_description != NULL) *error_description = "No Error"; if (!curl) { return false; } CURLcode err_code = CURLE_OK; (curl_easy_setopt)(curl, CURLOPT_URL, url.c_str()); (curl_easy_setopt)(curl, CURLOPT_USERAGENT, kUserAgent); // Set proxy information if necessary. if (!proxy.empty()) (curl_easy_setopt)(curl, CURLOPT_PROXY, proxy.c_str()); if (!proxy_user_pwd.empty()) (curl_easy_setopt)(curl, CURLOPT_PROXYUSERPWD, proxy_user_pwd.c_str()); if (!ca_certificate_file.empty()) (curl_easy_setopt)(curl, CURLOPT_CAINFO, ca_certificate_file.c_str()); struct curl_httppost *formpost = NULL; struct curl_httppost *lastptr = NULL; // Add form file. (curl_formadd)(&formpost, &lastptr, CURLFORM_COPYNAME, "file", CURLFORM_FILE, upload_file.c_str(), CURLFORM_END); (curl_easy_setopt)(curl, CURLOPT_HTTPPOST, formpost); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "send", CURLFORM_END); // Disable 100-continue header. struct curl_slist *headerlist = NULL; char buf[] = "Expect:"; // struct curl_slist* (curl_slist_append)(struct curl_slist *, const char *); //*(void**) (&curl_slist_append) = dlsym(curl_lib, "curl_slist_append"); headerlist = (curl_slist_append)(headerlist, buf); (curl_easy_setopt)(curl, CURLOPT_HTTPHEADER, headerlist); if (response_body != NULL) { (curl_easy_setopt)(curl, CURLOPT_WRITEFUNCTION, WriteCallback); (curl_easy_setopt)(curl, CURLOPT_WRITEDATA, reinterpret_cast<void>(response_body)); } // Fail if 400+ is returned from the web server. (curl_easy_setopt)(curl, CURLOPT_FAILONERROR, 1); err_code = (curl_easy_perform)(curl); if (response_code != NULL) { (curl_easy_getinfo)(curl, CURLINFO_RESPONSE_CODE, response_code); } if (error_description != NULL) *error_description = (curl_easy_strerror)(err_code); (curl_easy_cleanup)(curl); if (formpost != NULL) { (curl_formfree)(formpost); } if (headerlist != NULL) { (curl_slist_free_all)(headerlist); } // dlclose(curl_lib); return err_code == CURLE_OK; } // static bool HTTPUpload::CheckParameters(const map<string string> ¶meters) { for (map<string string>::const_iterator pos = parameters.begin(); pos != parameters.end(); ++pos) { const string &str = pos->first; if (str.size() == 0) return false; // disallow empty parameter names for (unsigned int i = 0; i 127) { return false; } } } return true; } //重写把数据读入上传数据流函数 size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) { FILE *fptr = (FILE*)userp; size_t m_size = fread(buffer, size, nmemb, fptr); return m_size; } </string></string></void></char></string></vector></stdio.h></curl></curl></assert.h>
CCLOG("################################################################################################ Begin send dump file"); string strPath = "E:\\3.0.25.63.zip"; std::map<string string> parameters; string response, error; const char * url = "http://127.0.0.1/WD/ReceiveFile.php"; bool success = HTTPUpload::SendRequest(url, parameters, strPath.c_str(), //"upload_file_minidump", "", "","","", &response, NULL, &error);</string>
以上代码是个简单的demo,代码相当粗糙,但应该对大家的理解影响不打,代码还有很大的优化空间,大家可以进行优化和扩展空间,期望大家一起完善!