当前位置:首页 » 文件管理 » php微信上传图片

php微信上传图片

发布时间: 2022-11-16 03:59:37

‘壹’ 各位大神,微信小程序用户上传的图片怎么用php存储数据库

通常是将图片的地址存储到数据库,上传之后,应该能获得图片的保存地址,然后把这个图片地址以字符串形式存储到数据库里。

‘贰’ PHP 微信上传永久素材

http请求方式: POST/FORM
http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
调用示例(使用curl命令,用FORM表单方式上传一个多媒体文件):
curl -F [email protected] "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE"

你这种情况属于使用curl命令的方式传值,帮助文档:http://linux.51yip.com/search/curl

正确情况下的返回JSON数据包结果如下:{"type":"TYPE","media_id":"MEDIA_ID","created_at":123456789}
错误情况下的返回JSON数据包示例如下(示例为无效媒体类型错误):{"errcode":40004,"errmsg":"invalid media type"}
另外全局返回码说明:https://mp.weixin.qq.com/wiki/17/.html

‘叁’ PHP 微信端上传图片,上传logo和banner图,哪位大神传授一下经验啊,有demo更好,可以加分

<divclass="header">
<span>头像</span>
<divclass="head_r"style="position:relative">
<imgclass="portrait_line"id="show_portrait"src="{$agent['portrait']}">
<imgsrc="__IMAGES__/more.png">
<inputtype="file"name="portrait"id="portrait"data-server="{:U('Home/Upload/mobile_upload_portrait')}"style="width:100%;height:100%;position:absolute;left:0px;top:0px;opacity:0;">
</div>
</div>

最后一个input file弄成透明的,占据整个你要触发上传的位置。

重点在下面,用原生ajax上传提交图片,并把上传后服务器本地的地址传回来,通过js付到表单里,并把图片缩小预览贴出来。

<script>
$(function(){
$("input#portrait").on("change",function(){
changepic('portrait','show_portrait');
});
});

varxhr;
varreturnimg="";
varreturninput="";

functionchangepic(id,img,input){
returnimg=img;
returninput=input;
varfileObj=document.getElementById(id).files[0];
varuploadServer=$("#"+id).attr("data-server");
varform=newFormData();
form.append("portrait",fileObj);
createXMLHttpRequest();
xhr.onreadystatechange=handleStateChange;
xhr.open("post",uploadServer,true);
xhr.send(form);
}

functioncreateXMLHttpRequest()
{
if(window.ActiveXObject)
{
xhr=newActiveXObject("Microsoft.XMLHTTP");
}
elseif(window.XMLHttpRequest)
{
xhr=newXMLHttpRequest();
}
}
functionhandleStateChange()
{
var$loading=layer.open({type:2,})
if(xhr.readyState==4)
{
if(xhr.status==200||xhr.status==0)
{
varresult=xhr.responseText;
varjson=eval("("+result+")");
if(json.result=='success'){
$.ajax({
type:'post',
url:'/index.php/Wap/Self/updatePortrait',
data:{
portrait:json.url,
},
success:function(){

},
error:function(){
alert('服务器错误');
},
});
$("#"+returnimg).attr("src",json.url);
layer.close($loading);
}
else{
alert('上传头像失败'+json.msg);
layer.close($loading);
}

}
}
}
</script>

然后是ajax上传的接口

publicfunctionmobile_upload_portrait(){//手机端上传头像
if(IS_POST){
$upload=newUpload();
$upload->maxSize=3*1024*1024;//3M
$upload->exts=array('jpg','gif','png','jpeg');
$upload->rootPath='./';
$upload->savePath='/Uploads/';
$upload->autoSub=true;
$upload->subName=array('date','Ymd');
$upload->saveName='uniqid';
if(!is_dir($upload->savePath)){
mkdir($upload->savePath);
}
$info=$upload->uploadOne($_FILES['portrait']);
if(!$info){
$result=array('result'=>'fail','msg'=>'请上传3M以下的图片');
}else{
$result=array('result'=>'success','url'=>$info['savepath'].$info['savename']);
}
$this->ajaxReturn($result);
}
}

代码是Thinkphp的项目截出来的,页面上有模板的痕迹,最后一段php的接口,也用了tp自带的文件上传类。不过看得懂的话,这些都不影响理解。

‘肆’ 微擎系统 微信端上传图片失败怎么办

如果是表单上传,要加上enctype="multipart/form-data",我经常忘记!!

‘伍’ 微信小程序如何使用PHP实现文件上传

调用小程序文件上传api
服务器接收到微信post过来的文件之后,php处理代码和传统文件上传代码一样

‘陆’ 如何在微信公众平台上传图片

1、首先进入微信公众平台官网。登陆微信公众账号,输入账号和密码,如果是自己电脑可以记住密码。

‘柒’ php微信上传永久图片素材求代码

您好,这样的:
//素材
const MEDIA_FOREVER_UPLOAD_URL = '/material/add_material?';
const MEDIA_FOREVER_NEWS_UPLOAD_URL = '/material/add_news?';
const MEDIA_FOREVER_NEWS_UPDATE_URL = '/material/update_news?';
const MEDIA_FOREVER_GET_URL = '/material/get_material?';
const MEDIA_FOREVER_DEL_URL = '/material/del_material?';
const MEDIA_FOREVER_COUNT_URL = '/material/get_materialcount?';
const MEDIA_FOREVER_BATCHGET_URL = '/material/batchget_material?';

/**
* 上传临时素材,有效期为3天(认证后的订阅号可用)
* 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
* 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
* 注意:临时素材的media_id是可复用的!
* @param array $data {"media":'@Path\filename.jpg'}
* @param type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
* @return boolean|array
*/
public function uploadMedia($data, $type){
if (!$this->access_token && !$this->checkAuth()) return false;
//原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}

/**
* 获取临时素材(认证后的订阅号可用)
* @param string $media_id 媒体文件id
* @param boolean $is_video 是否为视频文件,默认为否
* @return raw data
*/
public function getMedia($media_id,$is_video=false){
if (!$this->access_token && !$this->checkAuth()) return false;
//原先的上传多媒体文件接口使用 self::UPLOAD_MEDIA_URL 前缀
//如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
$result = $this->http_get($url_prefix.self::MEDIA_GET_URL.'access_token='.$this->access_token.'&media_id='.$media_id);
if ($result)
{
if (is_string($result)) {
$json = json_decode($result,true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
}
return $result;
}
return false;
}

/**
* 上传永久素材(认证后的订阅号可用)
* 新增的永久素材也可以在公众平台官网素材管理模块中看到
* 注意:上传大文件时可能需要先调用 set_time_limit(0) 避免超时
* 注意:数组的键值任意,但文件名前必须加@,使用单引号以避免本地路径斜杠被转义
* @param array $data {"media":'@Path\filename.jpg'}
* @param type 类型:图片:image 语音:voice 视频:video 缩略图:thumb
* @param boolean $is_video 是否为视频文件,默认为否
* @param array $video_info 视频信息数组,非视频素材不需要提供 array('title'=>'视频标题','introction'=>'描述')
* @return boolean|array
*/
public function uploadForeverMedia($data, $type,$is_video=false,$video_info=array()){
if (!$this->access_token && !$this->checkAuth()) return false;
//#TODO 暂不确定此接口是否需要让视频文件走http协议
//如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
//$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
//当上传视频文件时,附加视频文件信息
if ($is_video) $data['description'] = self::json_encode($video_info);
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}

/**
* 上传永久图文素材(认证后的订阅号可用)
* 新增的永久素材也可以在公众平台官网素材管理模块中看到
* @param array $data 消息结构{"articles":[{...}]}
* @return boolean|array
*/
public function uploadForeverArticles($data){
if (!$this->access_token && !$this->checkAuth()) return false;
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPLOAD_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}

/**
* 修改永久图文素材(认证后的订阅号可用)
* 永久素材也可以在公众平台官网素材管理模块中看到
* @param string $media_id 图文素材id
* @param array $data 消息结构{"articles":[{...}]}
* @param int $index 更新的文章在图文素材的位置,第一篇为0,仅多图文使用
* @return boolean|array
*/
public function updateForeverArticles($media_id,$data,$index=0){
if (!$this->access_token && !$this->checkAuth()) return false;
if (!isset($data['media_id'])) $data['media_id'] = $media_id;
if (!isset($data['index'])) $data['index'] = $index;
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}

/**
* 获取永久素材(认证后的订阅号可用)
* 返回图文消息数组或二进制数据,失败返回false
* @param string $media_id 媒体文件id
* @param boolean $is_video 是否为视频文件,默认为否
* @return boolean|array|raw data
*/
public function getForeverMedia($media_id,$is_video=false){
if (!$this->access_token && !$this->checkAuth()) return false;
$data = array('media_id' => $media_id);
//#TODO 暂不确定此接口是否需要让视频文件走http协议
//如果要获取的素材是视频文件时,不能使用https协议,必须更换成http协议
//$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_GET_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
if (is_string($result)) {
$json = json_decode($result,true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return $result;
}
return false;
}

/**
* 删除永久素材(认证后的订阅号可用)
* @param string $media_id 媒体文件id
* @return boolean
*/
public function delForeverMedia($media_id){
if (!$this->access_token && !$this->checkAuth()) return false;
$data = array('media_id' => $media_id);
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_DEL_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return true;
}
return false;
}

/**
* 获取永久素材列表(认证后的订阅号可用)
* @param string $type 素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news)
* @param int $offset 全部素材的偏移位置,0表示从第一个素材
* @param int $count 返回素材的数量,取值在1到20之间
* @return boolean|array
* 返回数组格式:
* array(
* 'total_count'=>0, //该类型的素材的总数
* 'item_count'=>0, //本次调用获取的素材的数量
* 'item'=>array() //素材列表数组,内容定义请参考官方文档
* )
*/
public function getForeverList($type,$offset,$count){
if (!$this->access_token && !$this->checkAuth()) return false;
$data = array(
'type' => $type,
'offset' => $offset,
'count' => $count,
);
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_BATCHGET_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}

/**
* 获取永久素材总数(认证后的订阅号可用)
* @return boolean|array
* 返回数组格式:
* array(
* 'voice_count'=>0, //语音总数量
* 'video_count'=>0, //视频总数量
* 'image_count'=>0, //图片总数量
* 'news_count'=>0 //图文总数量
* )
*/
public function getForeverCount(){
if (!$this->access_token && !$this->checkAuth()) return false;
$result = $this->http_get(self::API_URL_PREFIX.self::MEDIA_FOREVER_COUNT_URL.'access_token='.$this->access_token);
if ($result)
{
$json = json_decode($result,true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}

‘捌’ 用php上传图片怎么做

上传图片原理:首先判断文件类型是否为图片格式,若是则上传文件,然后重命名文件(一般都是避免上传文件重名,现在基本上都是以为时间来命名),接着把文件上传到指定目录,成功上传后输出上传图片的预览。

1.首先我们开始判断文件类型是否为图片类型用到的函数

{
strrchr:查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符。
substr: 取部份字符串。
$HTTP_POST_FILES['file']['name']:获取当前上传的文件全称。
}
图片类型就是“.”后面的字符(比如:一个文件名称为XXX.JPG 那么它的类型就是“.”后面的JPG)。 我们可以用PHP中的函数来截取上传者文件名字的。我们来写个获取文件类型的函数

<?
function type()
{
return substr(strrchr($HTTP_POST_FILES['file']['name'],'.'),1);
}
?>
2.若是则上传文件,然后重命名文件用到的函数

{ strtolower:把字符串的字母全部转换为小写字母. in_array: 函数在数组中搜索给定的值。 implode:函数把数组元素组合为一个字符串 random:随机生成的数 $_FILES['userfile']['name']:上传文件名称 $uploaddir:自己定义的变量。比如在同一个文件夹里面,你想把上传的文件放到这个文件夹的FILE文件夹下,你可以这样定义$uploaddir="./file/";注意写法 } 这边会出现很多问题,第一先写一个能上传类型的数组。第二判断文件合法性。第三给文件重名。*(这边判断文件大小就不写了)先定义允许上传文件的类型数组:$type=array("jpg","gif","bmp","jpeg","png");第二用一个IF。。else。。写一个判断文件合法性的控制流语句。if(!in_arry(strtolower(type()),$type))//如果不存在能上传的类型 { $text=implode('.',$type); echo "您只能上传以下类型文件: ",$text,"<br>"; } 下面就是给他们重新命名了,else { $filename=explode(".",$_FILES['userfile']['name']);//把上传的文件名以“.”好为准做一个数组。 $time=date("m-d-H-i-s");//去当前上传的时间 $filename[0]=$time;//取文件名t替换 name=implode(".",$filename); //上传后的文件名 $uploadfile=$uploaddir.$name;//上传后的文件名地址 } 3.最后把文件上传到指定目录,成功上传后输出上传图片的预览用到的函数{ move_uploaded_file:执行上传文件 } if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadfile)) { echo "<center>您的文件已经上传完毕 上传图片预览: </center><br><center><img src='$uploadfile'></center>"; echo"<br><center><a href='javascrīpt:history.go(-1)'>继续上传</a></center>"; } else { echo"传输失败!"; }

‘玖’ php开发微信jssdk接口 iphone手机当一次多图上传时,只有一张上传成功怎么回事,安

微信上传图片,只能递归方法上传多张,所以你可能是JS代码里只上传了一次

‘拾’ PHP微信开发上传永久缩略图(thumb)失败

你要想永久,就放自己服务器,
在微信上面调用自己服务器的图片就好了

热点内容
安卓怎么关闭美易订阅 发布:2024-05-18 19:29:16 浏览:642
苹果手机配置代理服务器怎么开 发布:2024-05-18 19:29:07 浏览:229
服务器屏蔽了别人的ip 发布:2024-05-18 19:10:09 浏览:619
怎么获取ins服务器地址 发布:2024-05-18 19:10:03 浏览:30
仙方一般是什么配置 发布:2024-05-18 18:53:43 浏览:159
黑莓安卓手机主题下载到哪里 发布:2024-05-18 18:47:18 浏览:57
汤靖轩编程 发布:2024-05-18 18:46:04 浏览:533
脚本故事梗 发布:2024-05-18 18:29:02 浏览:823
安卓和csharp哪个发展好 发布:2024-05-18 18:09:30 浏览:527
换编程题库 发布:2024-05-18 18:00:58 浏览:562