微信小程序上传图片到php服务器的方法
本文实例为大家分享了微信小程序上传图片到php服务器的具体代码,供大家参考,具体内容如下
js代码如下
submitPhoto(){ varthat=this; wx.uploadFile({ url:'http://xxx.cn/upload.php',//仅为示例,非真实的接口地址 filePath:imagePath, name:'imgfile', success:function(res){ vardata=JSON.parse(res.data);; console.log(data); //dosomething if(data.code==1){ wx.showToast({ title:'成功', icon:'success', duration:1000 }) } } }) },
PHP代码如下upload.php
$code, 'message'=>$message, 'data'=>$data ); //输出json echojson_encode($result); exit; } } $uplad_tmp_name=$_FILES['imgfile']['tmp_name']; $uplad_name=$_FILES['imgfile']['name']; $image_url=""; //上传文件类型列表 $uptypes=array( 'image/jpg', 'image/jpeg', 'image/png', 'image/pjpeg', 'image/gif', 'image/bmp', 'image/x-png' ); //图片目录 $img_dir="upload/"; //……html显示上传界面 /*图片上传处理*/ //把图片传到服务器 //初始化变量 $date=date(ymdhis); $uploaded=0; $unuploaded=0; //上传文件路径 $img_url="http://test.cayden.cn/upload/"; //如果当前图片不为空 if(!empty($uplad_name)) { //判断上传的图片的类型是不是jpg,gif,png,bmp中的一种,同时判断是否上传成功 //if(in_array($_FILES['imgfile']["type"][$i],$uptypes)) //{ $uptype=explode(".",$uplad_name); $newname=$date."-0".".".$uptype[1]; //echo($newname); $uplad_name=$newname; //如果上传的文件没有在服务器上存在 if(!file_exists($img_dir.$uplad_name)) { //把图片文件从临时文件夹中转移到我们指定上传的目录中 $file=$img_dir.$uplad_name; move_uploaded_file($uplad_tmp_name,$file); chmod($file,0644); $img_url1=$img_url.$newname; $uploaded++; Response::json(1,'success',$img_url1); } //} //else //{ //Response::json(1,'typeerror',$img_url1); //$unuploaded++; //} }//endif Response::json(0,'error',$img_url1); ?>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。