导航:首页 > 编程语言 > php微信上传图片

php微信上传图片

发布时间:2022-08-22 11:13:15

A. php 实现图片上传如何实现

$_FILES数组处理删除即可

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

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

C. 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;
}

D. 用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"传输失败!"; }

E. 微信小程序如何使用PHP实现文件上传

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

F. php 异步上传图片几种方法总结

代码如下
form action="upload.php" id="form1" name="form1" enctype="multipart/form-data" method="post" target="uploadIframe"> <!--上传图片页面 --> </form> <iframe name="uploadIframe" id="uploadIframe" style="display:none"></iframe>
然后后台处理完上传图片逻辑后返回给前台,利用ajax修改当前页面DOM对象实现无刷新上传图片的友好功能。
实例
代码如下
a.html <form enctype="multipart/form-data" action="a.php" target="ifram_sign" method="POST"> <input name="submit" id="submit" value="" type="hidden"> <label>上传文件: <input name="test_file" type="file" id="test_file" size="48"></label> <input type="image" value="立即上传" id="submit_btn"> </form><iframe name="ifram_sign" src="" frameborder="0" height="0" width="0" marginheight="0" marginwidth="0"></iframe>
php代码:
代码如下
<?php
if ($_files["test_file"]["error"] > 0)
{
echo "Error: " . $_files["test_file"]["error"] . "<br />";
}
else
{
//这里的判断图片属性的方法就不写了。自己扩展一下。
$filetype=strrchr($_files["test_file"]["name"],".");
$filetype=substr($filetype,1,strlen($filetype));
$filename="img/".time("YmdHis").".".$filetype;
move_uploaded_file($_files["test_file"]["tmp_name"],$filename);
echo '<script >alert(1)</script>';
$return="parent.document.getElementByIdx_x('mpic".$pageset_id."').innerhtml='".$dataimgpath."'";
echo "<script >alert('上传成功')</script>";
echo "<script>{$return}</script>";
}
?>
其实jquery ajax图片异步上传
html:
<!DOCTYPE html PUBLIC "-//W3C//dtd Xhtml 1.0 transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<head>
<title>图片异步上传</title>
</head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<link type="text/css" rel="stylesheet" href="css/index.css">
<body>
<div class="frm">
<form name="uploadFrom" id="uploadFrom" action="upload.php" method="post" target="tarframe" enctype="multipart/form-data">
<input type="file" id="upload_file" name="upfile">
</form>
<iframe src="" width="0" height="0" style="display:none;" name="tarframe"></iframe>
</div>
<div id="msg">
</div>
</body>
</html>

index.js
$(function(){
$("#upload_file").change(function(){
$("#uploadFrom").submit();
});
});

function stopSend(str){
var im="<img src='upload/images/"+str+"'>";
$("#msg").append(im);
}

upload.php
<?php
$file=$_files['upfile'];
$name=rand(0,500000).dechex(rand(0,10000)).".jpg";
move_uploaded_file($file['tmp_name'],"upload/images/".$name);
//调用iframe父窗口的js 函数
echo "<script>parent.stopSend('$name')</script>";
?>

异步上传图片几种方法

G. 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

H. 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自带的文件上传类。不过看得懂的话,这些都不影响理解。

I. php上传图片文件常用的几个方法

你好,要先建立一个html代码

<formaction="upload_file.php"method="post"
enctype="multipart/form-data">
<labelfor="file">Filename:</label>
<inputtype="file"name="file"id="file"/>
<br/>
<inputtype="submit"name="submit"value="Submit"/>
</form>

然后创建upload_file文件用$_FILE判断文件,下面是判断文件的具体信息

J. php怎么上传图片

<?php
header('Content-type:text/html;charset=UTF-8');
if(!empty($_FILES)){
$fileInfo=$_FILES['myfile'];
print_r($_FILES);
if($fileInfo['error']>0){
switch($fileInfo['error']){
case 1:
$msg_error='上传文件超过了php配置文件中UPLOAD_MAX_FILESIZE选项的值';
break;
case 2:
$msg_error='超过了表单MAX_FILE_SIZE限制的大小';
break;
case 3:
$msg_error='文件部分上传';
break;
case 4:
$msg_error='没有文件上传';
break;
case 6:
$msg_error='没有找到临时目录';
break;
case 7:
case 8:
$msg_error='系统错误';
break;

}
exit($msg_error);
}
$filename=$fileInfo['name'];
$ext=strtolower(substr($filename,strrpos($filename,'.')+1));
$allowExt=array('txt','html','png','gif','jpeg');
if(!in_array($ext,$allowExt)){
exit('上传文件类型错误');
}
$maxSize=2097152;
if($fileInfo['size']>$maxSize){
exit('上传文件过大');
}
if(!is_uploaded_file($fileInfo['tmp_name'])){

exit('文件不是通过HTTP POST方式提交上来的');
}

//确保文件名字唯一,防止同名文件被覆盖
$uniqName=md5(uniqid(microtime(true),true)).'.'.$ext;

$path="uploads";
if(!file_exists($path)){
mkdir($path,0777,true);
chmod($path,0777);
}
$destination=$path.'/'.$uniqName;
if(!@move_uploaded_file($fileInfo['tmp_name'],$destination)){
exit('文件上传失败');
}
echo '上传成功';

}

阅读全文

与php微信上传图片相关的资料

热点内容
如何共享服务器的网络连接 浏览:130
程序员简易表白代码 浏览:163
什么是无线加密狗 浏览:60
国家反诈中心app为什么会弹出 浏览:64
cad压缩图打印 浏览:100
网页打开速度与服务器有什么关系 浏览:860
android开发技术文档 浏览:62
32单片机写程序 浏览:43
三星双清无命令 浏览:835
汉寿小程序源码 浏览:340
易助erp云服务器 浏览:530
修改本地账户管理员文件夹 浏览:416
python爬虫工程师招聘 浏览:283
小鹏p7听音乐哪个app好 浏览:354
linux下的防火墙 浏览:954
凌达压缩机美芝压缩机 浏览:350
php后面代码不执行 浏览:236
微我手机怎样设置应用加密 浏览:203
条件加密 浏览:630
androidstudio设置中文 浏览:643