[C++][PHP]C++使用liburl上传图片,PHP接受图片并保存,再下发展示图片的网址

1.index.php

 <?php

    if(!empty($_GET['Img']))
    {
    	//判断当前是不是扫码模式,扫码后当前网页根据扫描的内容加载图片
    	//这里的图片没有重新命名,全叫Send.jpg
    	$ImgPath="<img src='Image/".$_GET['Img']."/"."Send.jpg' />";
    	echo $ImgPath;
    	echo "<title>测试</title>";
    	return;
    }
    if (!empty($_POST['title'])) 
    {
    	//POST过来的标题名字
    	$handle=fopen('title.txt','ab+');
    	fwrite($handle,$_POST['title']);
        fclose($handle);
    }

    if(empty($_FILES['img']['name']))
    {
    	//$_FILES:当上传单个文件时候,它是二维数组
    	//$_FILES['img'],img是post之前约定好的名字
    	//$_FILES['img']['name'],name是它已经拥有的名字
    	//$_FILES['img']['tmp_name'],上传的数据流,这里是图片
    	$handle=fopen('empty.txt','ab+');
    	fclose($handle);
    }else
    {
    	//生成一个独一无二的ID
    	$uniqid = md5(uniqid(microtime(true),true));
    	$dir='Image/'.$uniqid;
    	//根据ID创建文件夹
    	if (!file_exists($dir))
    	{
            mkdir ($dir,0777,true);
        }
        //保存图片,这里你可以重命名
    	$file='Image/'.$uniqid.'/'.$_FILES['img']['name'];
    	move_uploaded_file($_FILES['img']['tmp_name'],$file);
        //下发图片所在网址,采用GET
        $selfUrl=$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
        $imgUrl= 'http://'.$selfUrl.'?Img='.$uniqid;
    	header('Content-Type:application/json; charset=utf-8');
        $arr = array('url'=>$imgUrl);
        exit(json_encode($arr));
    }
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>测试</title>
	</head>
	  
	<body>
	</body>
</html>

2.C++ liburl(还在整理.......)

#include "stdafx.h"
#include "AnHttpPost.h"
#include "curl/curl.h"  

const std::string HTTP_SIGN_IMG = "img";
const std::string HTTP_SIGN_TITLE = "title";

AnHttpPost::AnHttpPost()
{

}


AnHttpPost::~AnHttpPost()
{
}

static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{
	std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);
	if (NULL == str || NULL == buffer)
	{
		return -1;
	}
	char* pData = (char*)buffer;
	str->append(pData, size * nmemb);
	return nmemb;
}

bool AnHttpPost::PostImg(const std::string & HttpUrl, const std::string & ImgFullPath, const std::string & WebTitle, std::string & Response)
{

	CURL* curl = curl_easy_init();
	if (NULL == curl)
	{
		return false;
	}

	curl_easy_setopt(curl, CURLOPT_URL, HttpUrl.c_str());//设置URL
	curl_easy_setopt(curl, CURLOPT_POST, 1);//使用POST
	
	struct HttpPost *formpost = NULL;
	struct HttpPost *lastptr = NULL;

	//添加图片,指定完整路径
	curl_formadd((curl_httppost**)&formpost,
		(curl_httppost**)&lastptr,
		CURLFORM_COPYNAME, HTTP_SIGN_IMG,
		CURLFORM_FILE, ImgFullPath.c_str(),
		CURLFORM_END);
	//添加标题
	curl_formadd((curl_httppost**)&formpost,
		(curl_httppost**)&lastptr,
		CURLFORM_COPYNAME, HTTP_SIGN_TITLE,
		CURLFORM_COPYCONTENTS, WebTitle.c_str(),//如果有中文记得自己转一下
		CURLFORM_END);

	//添加结束
	curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

	curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&Response);

	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);

	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

	//清除
	curl_easy_cleanup(curl);
	return true;
}

猜你喜欢

转载自blog.csdn.net/qq_36251561/article/details/119002614